AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to write Javascript validation for Email Address
Posted by vamshi on  10/20/2008 9:44:42 PM Category: JavaScript
Add to my favorites
Email to friend
hi iam using javascript for email validation.....my question is how to write javascript for email validation plese help me

Post Your Reply Post New Question

 Viewer's Reply
Simple Validation for Email Address
Answered By Moderator1 on 10/21/2008 8:22:35 AM
You can build this function as per your requirement.

function isEmail(frm,obj,txt)
{
var fld=frm[obj];
var str = fld.value;
if (str.indexOf(',',0) != -1) {
alert ("You Cannot Have a comma In Your Email Address..");
// fld.value=""
fld.focus();
return false;
}
else if (str.indexOf('/',0) != -1) {
alert ("You Cannot Have a \" / \" In Your Email Address.");
// fld.value=""
fld.focus();
return false;
}
else if (str.indexOf('+',0) != -1) {
alert ("You Cannot Have a \" + \" In Your Email Address.");
//fld.value=""
fld.focus();
return false;
}
else if (str.indexOf('@',0) == -1) {
alert ("You must enter a fully qualified email address (e.g. sales@yourwebsite.com).");
//fld.value=""
fld.focus();
return false;
}
else if (str.indexOf('.',0) == -1) {
alert ("You must enter a fully qualified email address (e.g. sales@yourwebsite.com).");
// fld.value=""
fld.focus();
return false;
}
else if (str.indexOf(' ') != -1) {
alert ("You must enter a fully qualified email address (e.g. sales@yourwebsite.com).");
// fld.value=""
fld.focus();
return false;
}
else { return true; }

}
 
JavaScript Email Validation with Regular Expression
Answered By Moderator1 on 10/21/2008 8:33:15 AM
function isEmail() {
emailRegex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var emailid = document.getElementById('txtEmailId').value;
if( !emailid.match( emailRegex ) )
{
alert( 'Please enter a valid Email Id' );
document.getElementById('txtEmailId').focus();
return false;
}
return true;
}

It works both in IE and FireFox.
 
validation for Email Address
Answered By Chapidi on 11/20/2008 5:01:41 AM
Hi,

function ValidateEmail(strEmail)
{
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(strEmail))
{ // nothing
return true;
}
else
return false;
}
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Many .NET teams are underperforming, is yours?
Washington Technology
Introduction to Software Development