Web Analytics Made Easy -
StatCounter Validating Email address !!! - CodingForum

Announcement

Collapse
No announcement yet.

Validating Email address !!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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. "[email protected]". It works. But when you put "[email protected]" 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; Jun 19, 2002, 03:53 PM.

  • #2
    <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; Jun 19, 2002, 04:37 PM.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          There is a regx one in this post:

          Comment


          • #6
            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 )
            jasonkarldavis.com

            Comment


            • #7
              That's a good one, jkd - but it said a friend of mine's email address, i.e.

              [email protected]

              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])[email protected][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. ;)

              Comment


              • #8
                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('[email protected]') == ['[email protected]', '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...
                jasonkarldavis.com

                Comment


                • #9
                  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. ;)

                  Comment


                  • #10
                    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.
                    jasonkarldavis.com

                    Comment


                    • #11
                      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

                      Comment


                      • #12
                        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

                        Comment


                        • #13
                          Ok, not to worry people, I figure my last problem out.

                          Here is what I should of put:

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

                          Thanks anyway

                          Comment

                          Working...
                          X