Try testing the following form with valid and invalid email addresses. The
code uses javascript to match the users input with a regular expression.
Function code:
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = [form_id].elements[email].value;
if((address) == false) {
alert('Invalid Email Address');
return false;
}
}
In the forms ‘onsubmit' code call javascript:return validate(‘form_id','email_field_id')
How to use:
<form method="post" action="" onsubmit="javascript:return validate('form_id','email');">
<input type="text" name="email" />
<input type="submit" value="Submit" />
</form>
You should not rely purely on client side validation on your website / web application, if the user has javascript disabled this will not work. Always validate on the server.
from: /blog/javascript-validation/
code uses javascript to match the users input with a regular expression.
Function code:
Copy the codeThe code is as follows:
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = [form_id].elements[email].value;
if((address) == false) {
alert('Invalid Email Address');
return false;
}
}
In the forms ‘onsubmit' code call javascript:return validate(‘form_id','email_field_id')
How to use:
Copy the codeThe code is as follows:
<form method="post" action="" onsubmit="javascript:return validate('form_id','email');">
<input type="text" name="email" />
<input type="submit" value="Submit" />
</form>
You should not rely purely on client side validation on your website / web application, if the user has javascript disabled this will not work. Always validate on the server.
from: /blog/javascript-validation/