codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » Javascript » Article

Javascript Email Validation function

Article by:  Jeff Anderson  ( 1361 ) (2/26/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: A javascript validation function to check whether the user has entered a valid email address in a form.
Viewed: 103819 times Rating (67 votes): 
 2.3 out of 5
 Rate this Article  Read Comments  Post Comments


There are many varieties of email validation. This one is simple, but effective. It checks for a period (.) character anywhere after the 3rd character in the string (this is very cautious, allowing for one letter domain names e.g. a@b.com; and for @ characters anywhere after the first character (more possible, as a single letter username is easily possible)

If both of these exist, the function returns true, otherwise it returns false.




function isValidEmail(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

}

To understand how to use the IsValidEmail function to validate a form, see Form Validation function Page.







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Javascript Email Validation function'
Posted by :  Archive Import (Richard Koudry) at 05:00 on Wednesday, September 18, 2002
This function is fantastic, but there is one problem:

It accepts an invalid email address, e.g. try@haveago.

is accepted as a valid address. The address should not finish with a dot unless I am mistaken.
Posted by :  Archive Import (Scott Kreischer) at 18:55 on Tuesday, October 22, 2002
<script language="javascript"><!--

function verifyEmail(form) {
checkEmail = form.email.value

if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
{alert("You have entered an invalid email address. Please try again.");
form.email.select();
return false;
}

else {
form.method="get";
form.target="_self";
form.action="myscript.cgi";
form.submit();
}

}
//--></script>
Posted by :  Archive Import (jantje) at 16:57 on Thursday, November 07, 2002
Leuk hoor
Posted by :  Archive Import (Bikash Biswas) at 02:34 on Saturday, November 30, 2002
There are certain points to be noted

1) What about domain names ending with .de , .se ,.in

2)The e-mail address like "bikash" is valid one. Try any unix box.

3)The e-mail address need not always contain "." . Again try any Unix box.
Posted by :  Archive Import (mamtha) at 04:17 on Wednesday, January 15, 2003
Scott Kreischer' code works for .in,.de etc
Posted by :  Archive Import (Paul) at 06:29 on Wednesday, February 19, 2003
function validate()
{
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
strEmail = document.form1.email.value;

if (strEmail.search(validRegExp) == -1)
{
alert(" A valid e-mail address is required.\r\Please check you have entered your details correctly.\r\r\ � Paul Holmes 2001-2003");
return false;
}
Posted by :  Archive Import (DinoBrain) at 14:08 on Wednesday, April 23, 2003
hi there...
when i enter an e-mail like this: dino@@yahoo.com .it's still accept this e-mail... can u make it better?
Posted by :  Archive Import (Pedro) at 14:11 on Monday, May 19, 2003
To validate email format, I've had luck with the following regular expression:

/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}$/i;

However, some browsers (Mac IE?) will result in an alert if there is an underscore. Any ideas why?
Posted by :  Archive Import (murali) at 04:34 on Saturday, June 21, 2003
THe email validation should check for the postition of @ and the (.) and the last 3 characters after dot.a maximum length of email not just @ and .
Posted by :  Archive Import (William) at 03:48 on Tuesday, August 12, 2003
this is the best I found and it works!


function isEmail(string) {
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
return true;
else
return false;
}




For the explanation:

http://tech.irt.org/articles/js049/index.htm
Posted by :  Archive Import (email_val) at 19:47 on Monday, August 25, 2003
Scott's stuff is better. But thanks anyways !
Posted by :  Archive Import (goddy) at 13:55 on Wednesday, August 27, 2003
i need the function of e-mail to be state out for me
Posted by :  sai_cool at 22:39 on Thursday, April 14, 2005
Can any body provide me a solution of running a c++ code using Java.Is there any command.


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
Javascript Onload Event
by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Javascript - Enable and Disable form elements
by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Form Validation Function
by Jeff Anderson
A javascript validation function that you can use to validate all types of forms.
Check IsNumeric Function
by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric.
JavaScript Field Is Empty Form Validation
by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not.
Check Email Validation Function
by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form.
Multiple submit buttons on a single form
by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button.
Validate Form and Disable Submit Button
by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating.
Simple date validation
by Chris Hogben
function that takes a date in three parts, day, month and year - returns true if it's a valid date.
Multiple submit buttons with form validation
by Paul Eckert
This code shows how to redirect a user to multiple sites, depending on which submit button he presses, but only after the form validates correctly.








Recent Forum Threads
• Re: sorting and Linked list
• Re: need help linked list
• Re: Help with arrays
• Re: Reading from a file
• Re: Why Use Method?
• Re: Help with a simple program
• Re: need help with quiz
• Re: Help with filesystem object & displaying in a table
• Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005