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 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Validating Email address !!!

    Hi people,

    I have found this script for validating an email address entered into a form field.
    Yet it seems in complete. When you enter e.g. "yourname@yahoo.com". It works. But when you put "yourname@.com" it still works. So it needs a bit adding to it. In the script it checks to make sure there are characters after the '.', but it needs a section to check that there are characters before the '.' also. I have tried figuring it out, but to no evail. Any one help me with this little addition.

    Thanks in advance




    oops, forgot the code the first time round when posting, here it is !!!!!


    function validEmail(email) {
    invalidChars = " /:,;"

    if (email == "") { // Cannot be empty
    return false
    }
    for (i=0; i<invalidChars.length; i++) { // Does it contain any invalid characters
    badChar = invalidChars.charAt(i)
    if (email.indexOf(badChar,0) > -1) {
    return false
    }
    }
    atPos = email.indexOf("@",1) // There must be at least one '@' symbol
    if (atPos == -1) {
    return false
    }
    if (email.indexOf("@",atPos+1) != -1) { // And only one '@' symbol
    return false
    }
    periodPos = email.indexOf(".",atPos) // at least one '.' after the '@'
    if (periodPos == -1) {
    return false
    }
    if (periodPos+3 > email.length) { // must be at least 2 characters after the '.'
    return false
    }
    return true
    }
    Last edited by waps; 06-19-2002 at 08:53 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <script>
    function check(eml) {
    invalidChars = " /:,;"

    msg = "";

    for (x=0; x<invalidChars.length; x++) {
    xchar = invalidChars.charAt(x);
    xchar2 = eml.indexOf(xchar);
    if (xchar2!=-1) {
    msg += "Invalid character "+xchar+" used.\n";
    }
    }

    eml2 = eml.split("@");
    l = eml2.length;
    if (l<2) {
    msg += "Missing \"@\" symbol.\n";
    }
    else if (l>2) {
    msg += "Cannot have more than one \"@\" symbol.\n";
    }else{
    eml3 = eml2[1].split(".");
    l2 = eml3.length;
    if (l2<2) {
    msg += "Missing \".\" symbol.\n";
    }
    else if (l2>2) {
    msg += "Cannot have more than one \".\" symbol in the sitename.\n";
    }else{
    if (eml3[0]=="") {
    msg += "Invalid site name.\n";
    }
    else if (eml3[1]=="") {
    msg += "invalid site extension.\n";
    }
    }
    }
    if (msg != "") {
    alert(msg);
    }else{
    alert("Valid email");
    }
    }
    </script>
    Last edited by x_goose_x; 06-19-2002 at 09:37 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The smallest I could get a basic test that checks for
    [character(s)] @ [character(s)] . [character(s)]

    was

    em = document.forms["reqs"].elements["email"].value;
    tr = (em.indexOf("@",1)>0) ? em.indexOf(".",3) : -1;
    if (tr<3) {
    alert('bad email');
    }

    I'm sure a regex will do better though.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    the windy city
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Ökii
    I'm sure a regex will do better though.
    actually, somebody posted an email validation script using regexp on old boards a ong time ago. i think it was jkd. i only wished i had it saved. may be somebody has it.
    Goals are dreams with deadlines
    -------------------------------------
    Nimlok Trade Show Display Booth and Exhibit
    http://www.orbusinc.com

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    There is a regx one in this post:

    what is this

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    How about http://www.javascriptkit.com/script/...2/acheck.shtml



    It contains the monster RegExp I wrote a while ago when I was bored:
    /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

    (I'm quite proud of that beast )

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's a good one, jkd - but it said a friend of mine's email address, i.e.

    someguy-lastname@whatever.com

    was invalid... from what I understand (correct me if I'm wrong!) email addresses should also allow a hyphen in the first part before the @ ?

    Anyway this one seems to work good:

    /^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}$/i
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Ah, \w doesn't incorpoate hyphens, thanks for the tip. (Didn't think of email addies with hyphens):

    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

    Fixes that (I designed the RegExp especially to be used like:

    re('user@domain.ext') == ['user@domain.ext', 'user', 'domain'. 'ext'];

    (or re.exec(str) if that's more comfortable syntax, but in actuality all RegExp instances are really functions, interestingly enough)

    That helps in validating certain parts of the address...

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still not totally convinced on the actual validation rules for email addresses, though...

    Does anyone have a comprehensive article on what exactly a valid email address is?!?

    I've looked and looked (since long before this post, fyi) and still... I've never found a comprehensive resource.

    Thanks in advance.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #10
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I'm pretty sure it just consists of a valid *nix username, @, a valid subdomain/domain combination, a ., then 2-6 letters, then optionally followed by a dot and 2 letters.

  • #11
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey thanks everyone for all your replys, I've put together a good email checker now from all of your input. Those regular expressions look pretty useful, must look into them abit more.

    Anyway thanks very much

  • #12
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi everyone,

    Im trying to create a regular expression to validate a phone number. Here is what I got:

    var te = /^([\d]+[?:\s][\d]+)$/

    if (!te.test(Email_Form.Telephone.value)) {
    alert ("Please enter your contact telephone number then re-submit the form")
    return false
    }

    What it is suppos to do, is limit the input to just digits, which it does, but to allow a space character, if the user choices to pu one in. So in which case, this: 01333 844444 would be ok, so would 01333844444. It only seems to work when a space is there.

    Any ideas ??

    Thanks

  • #13
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, not to worry people, I figure my last problem out.

    Here is what I should of put:

    /^([\d?:\s]{11,})$/

    Thanks anyway


  •  

    Posting Permissions

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