PDA

View Full Version : Create a Share Javascript Validation Function!


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!

Roy Sinclair
11-13-2002, 04:23 PM
Rather than reinvent the wheel I think I'll refer you to the code by CF Regular Peter Bailey

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

beetle
11-13-2002, 04:48 PM
Originally posted by Roy Sinclair
Rather than reinvent the wheel I think I'll refer you to the code by CF Regular Peter Bailey

http://www.peterbailey.net/fValidate/site.php Word up! :D

whammy
11-14-2002, 12:44 AM
No doubt... the only reason I'm making a function library for ASP is because I haven't seen one that's as good as what I've done so far.

Peter Bailey's javascript API seems to be pretty solid from what I've messed with, but since I use server-side scripting I don't validate with javascript much anymore, unless it has to be client-side - in which case I just do it. :)

Of course, once I get into ASP.NET more thoroughly I'll probably abandon the ASP functions... who knows though, my company won't be using .Net for awhile... :rolleyes: