?
Solved

Validate Email address (if one is submitted)

Posted on 2005-03-04
5
Medium Priority
?
156 Views
Last Modified: 2012-05-05
I would like to use JavaScript to validate the email on the current form, prior to submitting to the next form. I found plenty of resources but all are after submit click. Currently developing using ASP (classic)... and I could check it at the end of the process using ASP, but would rather not allow the invalid email on the first form, but the code has to allow for null (since it is an optional input).  Open to better ideas and example code (not reference links) welcome.
0
Comment
Question by:Hal9000Returns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
NETTY4 earned 500 total points
ID: 13462248
How about this:



<form>
<input type=text name="userEmail" size="60" onChange="if(this.value>''&&!this.value.match(/^\w+(\.\w+)*\@\w+(\.\w+)*\.\w{2,4}$/))alert('invalid email address.')">
</form>


0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13464995
try this:

<script>
function emailTest(obj){
reg=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
 if (!reg.test(obj.value) ){
  alert('Invalid email')
  obj.focus()
 }
}
</script>
<input type="text" onblur="emailTest(this)">E-Mail

Emails come in many many different forms and there is no known regex that will catch all correct ones and reject all bad ones. The best you can do is to eliminate as many wrong ones as you can but allow through all the correct ones.
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13464997
Here is a simple webpage that will produce a form that will test the email and validate it.  You may copy and paste it into your program:

<html><head><title>Validating an Email Address</title>
<script language = "JavaScript">
<!-- hide me
function checkEmail(the_email)
{
    var the_at = the_email.indexOf("@");
    var the_dot = the_email.lastIndexOf(".");
    var a_space = the_email.indexOf(" ");
    if ((the_at != -1) &&  // if there's an '@'
        (the_at != 0) &&  // and it's not at position 0
        (the_dot != -1) && // and there's a '.'
        (the_dot > the_at + 1) &&  // and something between the '@' and '.'
        (the_dot < the_email.length - 1) && // and something after the '.'
        (a_space  == -1))  // and there're no spaces
         {
                 alert("looks good to me!");
                 return true;
         }  else {
                 alert("sorry, your email address is invalid!");
                 return false;
         }
}
// show me -->
</script>
</head>
<body>
<form method="POST" action="#"
    onSubmit = "var result = checkEmail(this.emailbox.value); return result;">
Email Address: <input type="text" name = "emailbox"><br>
<input type="submit" value="submit me!">
</form>
</body>
</html>
0
 

Author Comment

by:Hal9000Returns
ID: 13469851
Thanks Netty4... simple yet effective.
0
 

Author Comment

by:Hal9000Returns
ID: 13506965
Well I was a little to quick to award... the first solution (although produces an alert) allows you to click the next. I need it to prevent the click submit (if and only if, it contains text) that is an invalid email.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question