Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    email validation help

    I am definitly a noob when it comes to js, but I was hoping that someone could look past that and suggest some advice to help complete this "simple" email validation.

    Here's what I have so far:

    if (document.myForm.email.value != email)
    {
    alert("Please provide a valid email address.")
    return false;
    }

    So I know what I need to do and that is create a variable called "email" that is equal to what an email address should have i.e. some text followed by an "@" symbol, followed by some text, followed by a ".", and finally followed by "com, net, org, gov, biz etc.

    I have no idea how to write this variable.

    I understand there are more thorough ways of doing this but this is really all I need.

    Any help would be greatly appreciated

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    How strange that no-one has ever asked this queston before!

    http://www.javascriptkit.com/script/...2/acheck.shtml

    or take the trouble to use the search feature of this forum.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No doubt, someone will suggest regular expressions, but unless you want to take the time to learn them, you will always be dependent on someone else to fix your code, if the error checking criteria changes.

    At any rate, below is a simple script, using variables, similar to what I use:


    Code:
    <script>
    <!--
    
    function chkFrm(){
    
    var doc = document['oFrm']['email'];
    var badChar = " /\\+()*&^%$#?<>=!~`[]|:;,'";
    
    if (doc.value == ""){
    alert("missing email...!");
    return false;
    }
    
    for (j=0;j<badChar.length;j++){
    var illChar = badChar.charAt(j);
    
    if (doc.value.indexOf(illChar,0) != -1){
    alert("email contains illegal character(s)...!");
    return false;
    }
    
    var atPos = doc.value.indexOf("@",1);
    
    if (atPos == -1){
    alert("missing, or illegal position of, \n \@ sign in email...!");
    return false;
    }
    
    var dotPos = doc.value.indexOf(".",3);
    
    if (dotPos == -1 || dotPos < atPos || dotPos < atPos+2){
    alert("missing, or illegal position of, \n dot (.) in email...!");
    return false;
    } else {
    document['oFrm'].submit();
    }
    }
    }
    
    //-->
    </script>
    
    <form name="oFrm" method="get" action="somefile.php">
    <input name="email" type="text" />
    <input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
    </form>
    This will check:

    1). If the email is empty.
    2). If the email contains any of the illegal characters listed.
    3). If the email contains an @ sign.
    4). If the @ sign is the first character.
    5). If the email contains a dot.
    6). If the dot comes before the @, or is the 1st character after the @.

    It's not comprehensive, but will pick up most common errors.


    -james
    Last edited by jamescover; 08-31-2004 at 07:56 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If it helps the regex for an email is
    Code:
    address.match(/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}$/i);
    I think I've got the syntax right for javascript

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by trib4lmaniac
    If it helps the regex for an email is
    Code:
    address.match(/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}$/i);
    I think I've got the syntax right for javascript
    I don't think underscores ever come after an @ symbol.... and.. percent symbols?
    and I prefer patteren.test(str) since it would only return true/false:
    http://devedge.netscape.com/library/...p.html#1194128

  • #6
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Come to think of it, I don't think you can have an underscore in the domain at all! Percentages for things like %20 (space) etc.

  • #7
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by trib4lmaniac
    Come to think of it, I don't think you can have an underscore in the domain at all! Percentages for things like %20 (space) etc.
    ...
    I didn't know spaces were allowed in an email address...... ?

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not spaces, it's entity. Which is %20.
    If you're going to be picky then I think almost any character can come before the @ symbol as long as it is between quotation marks. (ie. "my&very$long~and#legal (i think) email!"@address.com)
    Might not be though


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •