Jump to content

Validation


pritam79
 Share

Recommended Posts

I tried a few sample email addresses with the following email address validation javascript code.

<html><head><script type="text/javascript">function validate_required(field,alerttxt){with (field)  {  if (value==null||value=="")	{	alert(alerttxt);return false;	}  else	{	return true;	}  }}function validate_form(thisform){with (thisform)  {  if (validate_required(email,"Email must be filled out!")==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>

But even when I entered a wrong email address like- .@w. , there was no error even if the address was not valid. So how can a form be validated properly before sending the data to the server? Also, the form’s action field has ‘submit.htm’ and it posts to itself, how will the form get processed?

Link to comment
Share on other sites

This is a Javascript question, you posted in the PHP forum.If you want to validate an email address format you need to use a regular expression. Do a search for validating emails in Javascript with a regular expression and you'll find plenty of examples.

Also, the form’s action field has ‘submit.htm’ and it posts to itself, how will the form get processed?
It won't. Static HTML pages can't process forms.
Link to comment
Share on other sites

This is a Javascript question, you posted in the PHP forum.
A mistake, sorry..
If you want to validate an email address format you need to use a regular expression. Do a search for validating emails in Javascript with a regular expression and you'll find plenty of examples.
Would it be better to use PHP form validation and not consider javascript validation at all?
Link to comment
Share on other sites

PHP validation is more secure (compared to JS, which isn't at all), but JS validation can be friendlier. If you have time, a combination of both is best, but if you have to take one only, do PHP.

Link to comment
Share on other sites

PHP validation is more secure (compared to JS, which isn't at all), but JS validation can be friendlier.
ok, but if the validation is done in php, will that bring extra overhead on the server since the form would be validated only after posting, or there are methods to validate in php before posting?
Link to comment
Share on other sites

ok, but if the validation is done in php, will that bring extra overhead on the server since the form would be validated only after posting, or there are methods to validate in php before posting?
That's why Synook suggested that you use a combination of both JS and PHP. The JavaScript will definitely take load off of the server, that way you're not constantly sending validation requests to the server through PHP, but having JS only validation is not very safe, since anyone can just disable JS and continue on. Edited by RahXephon
Link to comment
Share on other sites

That's why Synook suggested that you use a combination of both JS and PHP. The JavaScript will definitely take load off of the server, that way you're not constantly sending validation requests to the server through PHP, but having JS only validation is not very safe, since anyone can just disable JS and continue on.
thanks
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...