...

View Full Version : form validation



hendryk
07-24-2002, 04:01 PM
Hi,

Just to let you know I am very new to Javascript and i need help.

I am trying to validate a form with lots of fields. I have to check that something has been entered in a large proportion of these fields. Is there a better way to do it than using lots of if statements i.e if (hospitalNo =="").........if(dob=="").........

K

jkd
07-24-2002, 08:34 PM
This belongs in the Javascript Programming forum. :)

Please try to post in the more appropriate forum next time, but don't worry about it. :)

:: moving thread ::

adios
07-24-2002, 08:45 PM
http://www.codingforums.com/showthread.php?threadid=2510

Let us know if you need to flag only certain fields as 'required'.

[isn't jkd polite?]

RadarBob
07-25-2002, 06:25 AM
I am trying to validate a form with lots of fields. I have to check that something has been entered in a large proportion of these fields. Is there a better way to do it than using lots of if statements i.e if (hospitalNo =="").........if(dob=="").........

It depends on just what you're validating against as to how you do it. Off had I'd say build a somewhat generic function since you are checking so many fields. Pass in two parameters, the value your checking and a list (array) of values you're validating against. This second parameter will be either a list of what's OK or a list of what's not OK - which ever is easier. Here's what the code might look like:


function validateField (thisfield, validValues) {
var IsValid = new Boolean (false);

for (var i = 0; i<validValues.length; i++) {
if thisfield == validValues[i].value
IsValid = true;
}
return IsValid;
} // function validateFields()


NOTE: if you're validating against a limited number of values, and all the fields you're checking are validated against the same values, then you can use a switch statement. Personally I like using switch statments whenever I can. I think they "self document" very well and are easy to add or delete cases from w/out touching the basic structure of the code. And ALWAYS include a default case.

NOTE: I may not have the right properties for array here, but you get the idea.
NOTE: Using the same basic code structure in the function above, you can use a regular expression instead of an array of values - again, it depends on what you're validating against.

RadarBob
07-25-2002, 06:35 AM
OK, another way, it checks all the fields at once.... IF all the fields are consecutive on the form then you can loop through the form.items and check against, say blank. Something like this. Again don't hold my feet to the fire on syntax; but this is doable.



function validateForm (thisform) {
FormIsValid = new Boolean (true);

for (var i=0; i<20; i++) {
if thisform.items[i].value == ""
FormIsValid = false;
}
return FormIsValid;
} // validateForm

premshree
07-25-2002, 08:09 AM
Here's a related URL :
http://www.qiksearch.com/javascripts/form-validator.htm

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum