Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining Conditional Statements

Status
Not open for further replies.

bill1one

Programmer
Sep 29, 2004
93
0
0
US
I'm trying to validate a form. I want the value entered into the textbox, Patron ID, to be greater than 20000000 and less than 21999999. I also want it to contain only numbers. Following is the code I'm working with.

function validate() {

if (document.PurchaseRequest.PatronID.value < 20000000 || document.PurchaseRequest.PatronID.value > 21999999) {
window.alert("Please enter a valid Patron ID.");
return false;

else {
if ((document.PurchaseRequest.PatronID.value / document.PurchaseRequest.PatronID.value) != 1) {
window.alert("Please enter a valid Patron ID.");
return false;
}

}

return true;
}

If I test the two condtions separately, they work, but when I combine them, they don't. Is it the way I'm using the else/if?

Any help is appreciated.
 
Take out the else statement. Just use two if statements and have return true at the end (like you do).
If either of the if statements evaluate false it is going to execute the return at that point anyway and never make it further into the code.


Paranoid? ME?? WHO WANTS TO KNOW????
 
BTW, I have been working on my own validator system.
One approach you might think about is this.
If you give your fields an ID tag then you can use document.getElementById('fieldname') to get at it's properties. You can create your own custom tag inside the element that contains a value telling it what type/types of validation to perform like: validator="not_empty,whole_number"

When you get the element with getElementById you can check to see if it has a validator tag, parse out the entries in that tag and apply each test appropriate for that tag.

Before submission of the form you can iterate through all of the fields on the form, check if they have the validator tag and if they do perform those validations against it.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top