Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 5th, 2003, 07:39 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Email Validate

How to validate given email in javascript?

Thanks,
Kannan
 
Old June 6th, 2003, 02:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

that depends on what kind of stuff you want to check in email. good start is detecting "@" and "." characters and also empty space " ". here's how you can do it:

function checkEmail(email) {
     if(email.indexOf("@")!=-1 && email.indexOf(".")!=-1 && email.indexOf(" ")!=-1) {
     alert("Specified email address is invalid");
     return false; }
     else alert("Good email address");
}

that should do it

the genuine genius
 
Old June 9th, 2003, 08:33 AM
Authorized User
 
Join Date: Jun 2003
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you really want to validate an email address, you might want to use a regular expression. See http://regexlib.com/ for examples of some regular expressions that validate email addresses.



-Van
(Old dog learning new tricks...)
 
Old July 3rd, 2003, 11:46 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my favorite method: (Caution: won't work if scripting is disabled!)

<html>

<head>
<title>Email Check</title>
</head>

<script language="JavaScript"><!--
function Form_Validator(theForm) {
    var regex = /^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,3}$/
    if (!regex.test(theForm.Email.value)) {
        alert("Invalid email address.");
        theForm.Email.focus();
        return (false);
    }
    return (true);
}
//-->
</script>

<body>
  <form method="POST" onsubmit="return Form_Validator(this)" action="javascript:alert('Email is valid')">
    <input name="Email" value="">
    <input type="submit" value="Validate Email">
  </form>
</body>

</html>






Similar Threads
Thread Thread Starter Forum Replies Last Post
validate email address dartcoach Javascript 0 April 28th, 2008 12:58 PM
Validate multiple email addresses mat41 Javascript 1 August 29th, 2007 12:45 AM
How to validate email address crmpicco Javascript How-To 2 February 16th, 2005 10:56 AM
Validate Email address richardtr BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 6 November 23rd, 2004 01:45 PM
Email Validate nskannan2007@yahoo.com HTML Code Clinic 3 June 9th, 2003 10:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.