charon
11-13-2002, 04:12 AM
I have a lot of forms whereby some of the forms have common fields like, name, ic, contact number and email adress.
For the uncommon fields are like Occupation, Company, Address and Comment. How do I create a function which can be used
for all the forms.
Normally I will use below Javascript for form fields validation.
function validateData()
{
var r1
var userName
var ic
r1 = new RegExp("[^a-zA-Z ]") //matches any character other than a-Z
userName = r1.test(document.formreg.Name.value)
ic = new String(document.formreg.IC.value)
if (document.formreg.Name.value == "")
{
alert("Please fill in your name.");
document.formreg.Name.focus();
}
else if ((document.formreg.Name.value != "") & (userName == true))
{
alert("Invalid user name.");
document.formreg.Name.focus();
}
else if ((document.formreg.TelHome.value != "") & (isNaN(document.formreg.TelHome.value) == true))
{
alert("Invalid contact number. Example:01258822000.");
document.formreg.TelHome.focus();
}
else if ((document.formreg.TelOffice.value != "") & (isNaN(document.formreg.TelOffice.value) == true))
{
alert("Invalid contact number. Example:60358822000.");
document.formreg.TelOffice.focus();
}
else if (document.formreg.Email.value == "")
{
alert("Please fill in your email address.");
document.formreg.Email.focus();
}
else if ((document.formreg.Email.value != "") & ((document.formreg.Email.value.indexOf("@") < 0 || document.formreg.Email.value.indexOf(".") < 0 )))
{
alert("Please fill in proper email addess.Example:tom@yahoo.com");
document.formreg.Email.focus();
}
else
{
document.formreg.submit();
}
}
But due to I have many forms with different fields, I need to rewrite the code for each form validation. It is not a profesional way and also wasting time.
Please advice!
For the uncommon fields are like Occupation, Company, Address and Comment. How do I create a function which can be used
for all the forms.
Normally I will use below Javascript for form fields validation.
function validateData()
{
var r1
var userName
var ic
r1 = new RegExp("[^a-zA-Z ]") //matches any character other than a-Z
userName = r1.test(document.formreg.Name.value)
ic = new String(document.formreg.IC.value)
if (document.formreg.Name.value == "")
{
alert("Please fill in your name.");
document.formreg.Name.focus();
}
else if ((document.formreg.Name.value != "") & (userName == true))
{
alert("Invalid user name.");
document.formreg.Name.focus();
}
else if ((document.formreg.TelHome.value != "") & (isNaN(document.formreg.TelHome.value) == true))
{
alert("Invalid contact number. Example:01258822000.");
document.formreg.TelHome.focus();
}
else if ((document.formreg.TelOffice.value != "") & (isNaN(document.formreg.TelOffice.value) == true))
{
alert("Invalid contact number. Example:60358822000.");
document.formreg.TelOffice.focus();
}
else if (document.formreg.Email.value == "")
{
alert("Please fill in your email address.");
document.formreg.Email.focus();
}
else if ((document.formreg.Email.value != "") & ((document.formreg.Email.value.indexOf("@") < 0 || document.formreg.Email.value.indexOf(".") < 0 )))
{
alert("Please fill in proper email addess.Example:tom@yahoo.com");
document.formreg.Email.focus();
}
else
{
document.formreg.submit();
}
}
But due to I have many forms with different fields, I need to rewrite the code for each form validation. It is not a profesional way and also wasting time.
Please advice!