• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Validate Email address (if one is submitted)

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
Hal9000Returns
Asked:
Hal9000Returns
  • 2
  • 2
1 Solution
 
NETTY4Commented:
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
 
ellandrdCommented:
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
 
ellandrdCommented:
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
 
Hal9000ReturnsAuthor Commented:
Thanks Netty4... simple yet effective.
0
 
Hal9000ReturnsAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now