PDA

View Full Version : Redundant validation code


charon
04-18-2003, 10:53 AM
Hi,

Due to I have a lot of forms which have same form fields most of the time, for instances:

form1:
Name
Ic
Sex,
Address


form2:
Name
Ic
Email
D.O.B
Contact_No

form3:
Name
Sex
Email
Passport
Occupation
Company

My question is How do I create a validation function which can apply to these three form's fields validation???? As you can see, there are fields in common for these forms->Name, Email, Ic and Sex.

Currently, my practise is, in order to prompt an error with alert message (Please enter your name, Invalid Name...etc), I create a validation function for these three forms respectively. I don't know it is a good practise or not, I'm now looking for better way of creating the validation function, so that there won't be redundancy.

Please advice!

Mr J
04-18-2003, 03:28 PM
Is the following any help?


<script>
function chkme(){
for(i=0;i<document.forms.length;i++){
for(j=0;j<document.forms[i].elements.length;j++){
if(document.forms[i].elements[j].value==""){
alert("Please enter some text in "+document.forms[i].elements[j].name +" "+ (i+1) +" box")
}
}
}
}
</script>

<form name="f1">
<input type="text" name="Name" value="">Name 1
</form>

<form name="f2">
<input type="text" name="Name" value="">Name 2
</form>

<form name="f3">
<input type="text" name="Name" value="">Name 3
<input type="button" value="Check" onclick="chkme()">
</form>

beetle
04-18-2003, 03:31 PM
You could use something like my fValidate script

http://www.peterbailey.net/fValidate/

charon
04-19-2003, 02:13 AM
hi, MrJ,

The problem is, some fileds are not required to fill in , moreover I still have to check if there are Valid input or not, like contact number must be numberic, name must be string, email address must have you@domainname.com.

My current functions are as below:
function validateData(theForm)
{
if (theForm.Name.value == "")
{
alert("Please fill in your name.");
theForm.Name.focus();
return (false);

}
else if ((theForm.Name.value != "") & (userName == true))
{
alert("Invalid user name.");
theForm.Name.focus();
return (false);

}
........
}

Every form has their own validation function

Please advice!

P/S:Thank beetle, I prefer my own scripting.:))