Validating an email address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MKO
    New Member
    • Aug 2007
    • 15

    #1

    Validating an email address

    Hi

    Again its probably easy, but here I am stuck on checking for these in an input.

    Can it be checked like this? Do I need to look at some other validation method for this?

    Code:
        
        if(( ! document.fmOrder.cdEmail.value == 0 ) && (does not contain an @ and one . in sequence))
        {
            errors.push("Please enter a valid email address");
        }
      
        
        
        if( errors.length > 0 )
        {
            alert(errors.join('\n'));
            return false;
        }
        
        return true;
    }
    Many thanks

    M
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by MKO
    Hi

    Again its probably easy, but here I am stuck on checking for these in an input.

    Can it be checked like this? Do I need to look at some other validation method for this?

    Code:
        
        if(( ! document.fmOrder.cdEmail.value == 0 ) && (does not contain an @ and one . in sequence))
        {
            errors.push("Please enter a valid email address");
        }
      
        
        
        if( errors.length > 0 )
        {
            alert(errors.join('\n'));
            return false;
        }
        
        return true;
    }
    Many thanks

    M
    You should use a regular expression for that. Google "email validation in Javascript".

    Comment

    • tj111
      New Member
      • Aug 2007
      • 12

      #3
      Use regular expressions for checking user input. They seem complicated at first, but once you understand the methods behind it, it makes alot of sense. If your new to regular expressions, give this (Regular Expressions Tutorial) a read, it really helps you understand them. Here's my email address verification script.

      [code=javascript]
      function isEmail (email) {
      var re =/^[A-Za-z0-9]+[\w.-]*?[A-Za-z0-9]+@[A-Za-z0-9]+[\w.-]*?\.[A-Za-z0-9]{2,5}$/;

      if (email.match(re )) {
      return true;
      }
      return false;
      }
      [/code]
      Then to check an email you call it like:
      [code=javascript]
      if (!isEmail(form. email.value)) {
      //Things to do if it doesn't match
      }
      //things to do if it matches
      [/code]


      Essentially it checks it like this:
      1. Starts with any letter or number (1 or more times)
      2. Can be followed by any letter, number, or the symbols "_", "-", or "."
      3. Contains any letter number 1 or more times immediately before the "@"
      4. Has "@" symbol
      5. Checks the same as 1-3 above again after the "@" symbol
      6. Ends with a "." followed by 2-5 letters or numbers (.com, .co.uk, etc)

      I didn't want email address to be able to start or end with "_" or "." because that is common among spammers, but very rare among standard email users.

      Comment

      • MKO
        New Member
        • Aug 2007
        • 15

        #4
        thanks both,

        I read up on it as suggested and its now working. Thanks heaps.

        tj - thanks for the logic behind the expression, it really helped me understand how it is working.

        Cheers again

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Changed thread title to better describe the problem.

          Comment

          • tj111
            New Member
            • Aug 2007
            • 12

            #6
            Yeah the logic makes it so much easier to write and understand regular expressions. Before I read that guide I knew how to write RegExp functions and such, but spent alot of time trying to figure out why certain things got through that shouldn't of and vice versa (and restraining myself from smashing my computer). Once you understand the methodology behind it, regular expressions become easy and very useful.

            Comment

            Working...