Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with JavaScript validation for multiple forms

Newbie
 
Join Date: Feb 2007
Posts: 4
#1: Feb 24 '07
Hello, Please help me with this code..I have 4 forms which link to different htmll page. I try to write one validation file that can validate for the form whenever user click on that form.How do I call Validate.js for each form? Please give me some hints.
Thanks

Validate.js
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//test version
//read the comment i put
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function do_checkEmail(x){
//do all email checking
if(x.value.indexOf("@")!= "-1" && x.value.indexOf(".") != "-1"){
return false;
}
else
{
return true;

}
}
function do_checkPhone(x){
//do all phone checking phone format 999-999-9999
if(x.value.length !=12){
return false;
}

for(var i = 0; i < x.value.length; i++){
if ((i>-1&&i<3)||(i>3 && i<7)||(i>7&& i>12)){
if(x.value.charAt(i) < "0" || x.value.charAt(i) > "9"){
return false;
}
}
//check for hyphen '-'
else if (x.value.charAt(i) != "-"){
return false;
}
}
return true;
}
//Check for empty field, last name and first name
function do_checkEmpty(x){
if (x.value == "" || x.value == "null"){
return false;
}
else{
return true;
}

}

function checkForm(form){

if (do_checkEmail(form.emailAdd) == false){
alert("Please enter a valid email.");
form.emailAdd.focus();
return false;
}
if (do_checkEmpty(form.lastname) == false){
alert("Please Enter Last Name.");
form.lastname.focus();
return false;
}
if (do_checkEmpty(form.firstname) == false){
alert("Please Enter First Name.");
form.firstname.focus();
return false;
}
return true;

}
//////////////////////////////////////////////////////////////////////

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Feb 24 '07

re: Need help with JavaScript validation for multiple forms


plz send ur html code ....

i am online ...
Newbie
 
Join Date: Feb 2007
Posts: 4
#3: Feb 24 '07

re: Need help with JavaScript validation for multiple forms


Quote:

Originally Posted by dmjpro

plz send ur html code ....

i am online ...

Thank for your help
here are the UR for two form
http://cs.metrostate.edu/~ics499sp0702/Khuong/Info.html
http://cs.metrostate.edu/~ics499sp0702/Khuong/Customer.html
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#4: Feb 25 '07

re: Need help with JavaScript validation for multiple forms


i don't understand how u link two html pages..
i think u are showing the pages in seperate windows

add a function in js ...

function myCheck(form_obj)
{
if(checkForm(form_obj)) document.forms[0].submit();
}

and edit ...

checkForm(form){

if (do_checkEmail(form.emailAdd) == false){
alert("Please enter a valid email.");
form.emailAdd.focus();
return false;
}
if (do_checkEmpty(form.lastname) == false){
alert("Please Enter Last Name.");
form.lastname.focus();
return false;
}
if (do_checkEmpty(form.firstname) == false){
alert("Please Enter First Name.");
form.firstname.focus();
return false;
}
return true;

}

before validate ...
check the existence of that object ..

if (typeof form.email != 'undefined' && Adddo_checkEmail(form.emailAdd) == false){
alert("Please enter a valid email.");
form.emailAdd.focus();
return false;
}

now try urself ... best of luck
Newbie
 
Join Date: Feb 2007
Posts: 4
#5: Feb 26 '07

re: Need help with JavaScript validation for multiple forms


Thank you much I will give it a try.

k
Reply