Jump to content

Form Validation


Fmdpa
 Share

Recommended Posts

<html><head><script type="text/javascript">function validate_email(field,alerttxt){with (field)  {  apos=value.indexOf("@");  dotpos=value.lastIndexOf(".");  if (apos<1||dotpos-apos<2)	{alert(alerttxt);return false;}  else {return true;}  }}function validate_form(thisform){with (thisform)  {  if (validate_email(email,"Not a valid e-mail address!")==false)	{email.focus();return false;}  }}</script></head><body><form action="submit.htm" onsubmit="return validate_form(this);" method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"></form></body></html>

By "return validate_form(this)" that means the entire form is being validated, right?

Edited by Fmdpa
Link to comment
Share on other sites

That means the entire form is passed to the validate_form() function... but then, if you look at the actual function, it proceeds to only pass the email field along to validate_email(), and simply returns the result of that.

Link to comment
Share on other sites

I have a form with a few fields. One of them is an e-mail address field. I want to validate the entire form to check that none of the fields are empty. But I also want to validate just the email field with the above mentioned validate_email() function. How do I do that?

Edited by Fmdpa
Link to comment
Share on other sites

you could probably just add extra functions for each additional field, set to validate in whatever manner you want, and add their function call in the validate_form function; similar to what's already happening now.

Link to comment
Share on other sites

Or you could use getElementsByTagName to automatically loop through all the fields, and then check them with one function, but if their name was "email" then check them with the email validating function instead.

Link to comment
Share on other sites

What did I do wrong here?

//check that the textfld contains something. function emptyField(textObj){	 if (textObj.value.length == 0) return true;	 for (var i=0; i<textObj.value.length; ++i) {		  var ch = textObj.value.charAt(i);		  if (ch != ' ' && ch != '\t') return false;	 }	 return true;}//check that the customer_mail (email address) field... //...is valid (characters in right place, e.g. aaa@aa.aa) function validate_email(field){with (field){apos=value.indexOf("@");dotpos=value.lastIndexOf(".");if (apos<1||dotpos-apos<2) {return false;}else {return true;}}}//final functionfunction vForm(formObj) {	 if (emptyField(formObj.subject))		  {alert("Please enter a subject.");}		  	 else if (emptyField(formObj.detail))		  {alert("Please enter your message.");}		  	 else if (emptyField(formObj.name))		  {alert("Please enter your name.");}		  	 else if (emptyField(formObj.customer_mail)&&validate_email(customer_mail)==false)		  {alert("Please enter your email address.");} 		  	 else {return true;}   	 return false;}

<form name="form1" method="post" action="send_contact.php" onsubmit="return vForm(document.form1);"><h3>Subject:</h3><input style="top:-40px;" name="subject" class="tb" type="text" id="subject" size="32"><h3 style="top:-35px;">Message:</h3><textarea style="top:-75px;" name="detail" class="tb" cols="32" rows="5" id="detail"></textarea><h3 style="top:-70px;">Name:</h3><input style="top:-110px;" name="name" class="tb" type="text" id="name" size="32"><h3 style="top:-110px;">Email:</h3><input style="top:-150px;" name="customer_mail" class="tb" type="text" id="customer_mail" size="32"><input type="reset" style="cursor:hand; position:relative; left:-30px; top:-105px;" alt="reset" title="Reset"  name="Submit2" value="Reset"><input style="cursor:hand; position:relative; left:-20px; top:-105px;" alt="submit" title="Submit e-mail" type="submit" name="Submit" value="Submit"></form>

Edited by Fmdpa
Link to comment
Share on other sites

What is not right?Either way, I think you should put parenthesis around the expression for the email, i.e.

else if (emptyField(formObj.customer_mail)&&(validate_email(customer_mail)==false))

Link to comment
Share on other sites

How about this?

else if (emptyField(formObj.customer_mail)||(validate_email(customer_mail)==false))

But it still doesn't work! The emptyField function works fine, but I can't get the validate_email function to cooperate! :)

Edited by Fmdpa
Link to comment
Share on other sites

I don't think the "&&" is a good idea though. The "||" was fine... still - good catch with the formObj.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...