Javascript allows different email addresses to PHP

Report issues with Enuuk Auction Platform or Mods here - remember to raise a ticket with phpauction.net as well
Post Reply
RWAP
Site Admin
Posts: 748
Joined: Fri Jan 08, 2010 2:23 am
Location: Stoke-on-Trent
Contact:

Javascript allows different email addresses to PHP

Post by RWAP » Mon Apr 08, 2013 1:33 pm

The check on the user's email address is different between the javascript version and the PHP version.

To overcome this, in themes\default\js\global.js

Find:

Code: Select all

    function validateEmail(){
        if (/^(.+\@.+\..+)$/.test(inputEmail.val())){
            reqEmail.removeClass("error");
            return true;
        } else {
            reqEmail.addClass("error");
            return false;
        }
    }
Change this to:

Code: Select all

    function validateEmail(){
        var customRegex='^([a-z\d])(([-a-z\d._-])*([a-z\d]))*\@'+
            '([a-z\d])(([a-z\d-])*([a-z\d]))+'+
            '(\.([a-z\d])([-a-z\d_-])?([a-z\d])+)+$';
        var re = new RegExp(customRegex,"i");
        if(re.test(inputEmail.val())){
            reqEmail.removeClass("error");
            return true;
        } else {
            reqEmail.addClass("error");
            return false;
        }   
    }
The regex used is exactly the same as the one used in the function String::isEmail()

RWAP
Site Admin
Posts: 748
Joined: Fri Jan 08, 2010 2:23 am
Location: Stoke-on-Trent
Contact:

Re: Javascript allows different email addresses to PHP

Post by RWAP » Wed Apr 24, 2013 8:20 am

Hmm for some reason, Javascript was changing the regex above, by replacing \d with just d in the string customRegex!!

No idea why this was.

The correct code is:

Code: Select all

    function validateEmail(){
        var customRegex=/^([a-z\d])(([-a-z\d._-])*([a-z\d]))*\@([a-z\d])(([a-z\d-])*([a-z\d]))+(\.([a-z\d])([-a-z\d_-])?([a-z\d])+)+$/i;
        if(customRegex.exec(inputEmail.val())){
            reqEmail.removeClass("error");
            return true;
        } else {
            reqEmail.addClass("error");
            return false;
        }   
    }


RWAP
Site Admin
Posts: 748
Joined: Fri Jan 08, 2010 2:23 am
Location: Stoke-on-Trent
Contact:

Re: Javascript allows different email addresses to PHP

Post by RWAP » Sat Aug 01, 2015 8:46 pm

This regex needs some improvement to handle long email addresses as they are typed without javascript hanging - try:

Code: Select all

    function validateEmail(){
        emailValue = inputEmail.val();
        if (emailValue.indexOf('@')=== -1) { 
            reqEmail.addClass("error");
            return false;
        }
        if (emailValue.indexOf(' ') !== -1) {
            // Space not allowed
            reqEmail.addClass("error");
            return false;
        }
        mailParts = emailValue.split('@');
        name = mailParts[0];
        domain = mailParts[1];
        if (domain.indexOf('.')=== -1) { 
            reqEmail.addClass("error");
            return false;
        }
        var customRegex=/^[a-z\d][a-z\d\._-]*[a-z\d]*$/i;
        if(!customRegex.exec(name)){
            reqEmail.addClass("error");
            return false;
        }
        domainParts = domain.split('.');
        for(var i=0; i < domainParts.length; i++) {
            var customRegex=/^[a-z\d][-a-z\d-]*[a-z\d]+$/i;
            if(!customRegex.exec(domainParts[i])){
                reqEmail.addClass("error");
                return false;
            }
        }
        reqEmail.removeClass("error");
        return true;
    }

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest