467,922 Members | 1,299 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,922 developers. It's quick & easy.

Need help with JavaScript validation for multiple forms

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;

}
//////////////////////////////////////////////////////////////////////
Feb 24 '07 #1
  • viewed: 1218
Share:
4 Replies
dmjpro
2GB
plz send ur html code ....

i am online ...
Feb 24 '07 #2
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
Feb 24 '07 #3
dmjpro
2GB
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
Feb 25 '07 #4
Thank you much I will give it a try.

k
Feb 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Jack Addington | last post: by
5 posts views Thread by Phil Powell | last post: by
27 posts views Thread by Chris | last post: by
2 posts views Thread by swethak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.