Jump to content

Validation


pritam79

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.
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...