SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Saturday, September 6, 2008

Email validation using regular expression with prototype

hi after working with prototype i find easy in working with client side validations.For validating a email field.we will be writing inline script or external script.Here i have used external js file for validation.For this form validation i have called all my external scripts inside the body.
For easy unsderstanding study prototype

1.Start designing the form



<html>
<title>Email Validation</title>
<head>

</head>
<body>
<form id="nwires" name='nwires' method="post" action="http://yoursite.com">
<input id="email_lookup" class="signin" type="text" value="Enter email here" title="Enter email here" size="30" name="email_lookup"/>
<input type="submit" value="Send" name="commit"/>
</form>
<script type='text/javascript' src='prototype.js'></script>
<script type='text/javascript' src='validate.js'></script>
</body>
</html>


2.Your external Validate.js should be



/*Function for Email validation */

$('nwires').onsubmit = function()

{

var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

str =$('email_lookup').value;

if(str.match(emailRegEx)){

$('nwires').submit();

}else{

alert('Please enter a valid email address.');

return false;

}

}

No comments :

Post a Comment