Checking end of email address

Discussion in 'JavaScript' started by adamjblakey, Dec 1, 2010.

  1. #1
    Hi,

    I have a register form why i take an email address. I have validation running to check it is a valid email and there is something in the field but i only want a user to enter an email address which ends in .co.uk i have searched around but cannot find anything. Does anyone know how i would check to make sure the user has entered a .co.uk email address.

    Cheers,
    Adam
     
    adamjblakey, Dec 1, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    http://www.jsfiddle.net/dimitar/JGrBM/ - this is a proper, RFC compliant valid email test which will ALLOW all the example emails i have included.

    i have also added a check to see if it's a .co.uk specifically.

    
    (function() {
        // define string prototypes for later testing.
        var validEmailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      
        String.prototype.isEmail = function() {
            // returns true if a string is a valid email
            return this.match(validEmailRegex);
        };
        
        String.prototype.isUKEmail = function() {
            // returns true if a string contains .co.uk
            return this.toLowerCase().indexOf(".co.uk") === -1 ? false : true;
        }
            
        String.prototype.isValidUKEmail = function() {
            // returns true if a mail is valid and uk.
            return this.isEmail() && this.isUKEmail();
        }
    })();
     
    // example use
    
    // data set
    var data = [
        '"Abc\@def"@example.com',
        '"Fred Bloggs"@example.com',
        '"Joe\\Blow"@example.com',
        '"Abc@def"@example.com',
        'customer/department=shipping@example.com',
        '$A12345@example.com',
        'foo@copmpany.co.uk',
        '!def!xyz%abc@example.com',
        '_somename@example.com'
     ];
    
    
    // analyze them all
    var count = data.length, isUK;
    while (count--) {
        if (!data[count].isEmail())
            document.write("INVALID: the email <strong>" + data[count] + "</strong> incorrectly reported as non-valid<br />");
        else {
            isUK = (data[count].isUKEmail()) ? 'UK' : 'foreign';
            document.write("OK: the email <strong>" + data[count] + "</strong> recognised correctly, " + isUK + "<br />");
        }
     };
     
    // exmaple where it only checks for a valid uk email.
    document.write("<br/><br/>");
    
    count = data.length;
    while (count--) {
        if (data[count].isValidUKEmail())
            document.write("UK email: <strong>" + data[count] + "</strong><br/>");
    }
     
    Code (javascript):
    the reason for this is simple: read http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
     
    Last edited: Dec 1, 2010
    dimitar christoff, Dec 1, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Will have to bookmark that one for future reference...
     
    camjohnson95, Dec 1, 2010 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Thanks for that :) As my JS is very limited, i am just wondering how i would call this? Or better yet just call the check if it is .co.uk feature as i have all the other validation in place.
     
    adamjblakey, Dec 1, 2010 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    sure. basically, it's called prototyping. each js entity (element, number, array, string etc) has a prototype chain which defines methods available any member object with the same constructor will inherit. For example, Array may have .forEach, String may have .toLowerCase() etc - all stored in the prototype. hence you can do "THIS IS CAPS".toLowerCase() which will return the string in lowercase or [1,2,4].forEach(function(nn, nnn) { ... }); will loop through all array elements and pass them to the callback function.

    we are adding a new method here to the string prototype. this means ANY string created can have this method applied to it (exactly like the .toLowerCase())

    eg:

    
    "foo@bar.co.uk".isValidUKEmail(); // returns true.
    
    // practical example
    var email = document.getElementById("email").value;
    
    if (email.isValidUKEmail()) {
    .... do something
    }
    else {
        alert("get a .co.uk email first!");
    }
    
    Code (javascript):
    of course, you can also use a normal function:
    
    var isUK_TLD = function(email) {
        return email.toLowerCase().indexOf(".co.uk") === -1 ? false : true;
    };
    
    alert(isUK_TLD("dimitar@foobar.co.uk")); // true
    
    Code (javascript):
     
    Last edited: Dec 1, 2010
    dimitar christoff, Dec 1, 2010 IP