-3

Possible Duplicate:
JQuery validate e-mail address regex

hi i have an input filed in a form referencing to an email input field , and when the user clicked the submit button i want to ensure that the email input field value has the formal like this

example@hotmail.com

or

example@gmail.com

or

example@yahoo.com

and the input field is this

<p>
            <label>Email</label>
            <input type="text" name="Email"/>
            <span class="errorMessage"></span>
        </p>

jquery code

$(document).ready(function(){
    $('#suform').on('submit', function(e){
        e.preventDefault();
        var errorCount = 0;
        $('span.errorMessage').text(''); // reset all error mesaage
        $('input').each(function(){
            var $this = $(this);
            if($this.val() === ''){
                var error = 'Please fill ' + $this.prev('label').text(); // take the input field from label
                $this.next('span').text(error);
                errorCount = errorCount + 1;   
            }
        });
        if(errorCount === 0){
            var mobileNumber = $('input[name=MNumber]');
            var email = $('input[name=Email]');
            if(isNaN(parseFloat(mobileNumber )) && !isFinite(mobileNumber )) {
                var error = 'Mobile number incorect.';
                $('input[name=MNumber]').next('span').text(error);
                errorCount = errorCount + 1; 
            }else{      
                var password= $('input[name="Password"]').val();
                var repass= $('input[name="RePassword"]').val();
                if(password!=repass){ // ensrue the two passwords are the same
                    var error2 = 'Password not matching';
                    $('input[name="RePassword"]').next('span').text(error2)
                    errorCount = errorCount + 1;  
                }else{
                    $(this)[0].submit(); // submit form if no error
                }
            }
        }
    });
});

my html , css and jquery code is here code

Community
  • 1
  • 1
William Kinaan
  • 28,059
  • 20
  • 85
  • 118

3 Answers3

2

Regular expression is one way to validate :

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\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,}))$/;
    return re.test(email);
} 

Pass a user entered email to this function it will check its formate and return true or false accordingly

Gaurav
  • 8,367
  • 14
  • 55
  • 90
2

If I understand you correctly, you want to validate the email address filled on the form:

ADD TO YOUR FUNCTION

// validate proper email address
var reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

if (reg.test(value) == false) {
  // email invalid, do stuff
} else {
  // email valid, do stuff
}

This Regular expression checks the email provided for many many many issues!

EDITED:

You're function had some typos, here it is fully functional: and a working Fiddle!

$(document).ready(function(){

    // form submit
    $('#suform').on('submit', function(e){

        // prevent default behavior
        e.preventDefault();

        // reset errors counter
        var errorCount = 0;

        // clear error message
        $('span.errorMessage').text('');

        // run by each input field to check if they are filled
        $('input').each(function(){

            var $this = $(this);

            if($this.val() === ''){
                // take the input field from label
                var error = 'Please fill ' + $this.prev('label').text();
                $this.next('span').text(error);
                errorCount = errorCount + 1;   
            }
        });

        // no errors so far, let continue and validate the contents
        if(errorCount === 0){

            // get mobile number
            var mobileNumber = $('input[name=MNumber]').val();

            // get email address
            var email = $('input[name=Email]').val();

            // get password and password repeat
            var password= $('input[name="Password"]').val();
            var repass= $('input[name="RePassword"]').val();

            // regular expression to validate the email address
            var reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

            // try to validate the email
            if (reg.test(email) == false) {
              $('input[name=Email]').next('span').text('Email address is invalid!');
              errorCount = errorCount + 1;
            } else {

                if(isNaN(parseFloat(mobileNumber )) && !isFinite(mobileNumber )) {
                    var error = 'Mobile number incorect.';
                    $('input[name=MNumber]').next('span').text(error);
                    errorCount = errorCount + 1;
                } else {
                    // ensrue the two passwords are the same
                    if(password!=repass){
                        var error2 = 'Password not matching';
                        $('input[name="RePassword"]').next('span').text(error2);
                        errorCount = errorCount + 1;  
                    }else{
                        $(this)[0].submit(); // submit form if no error
                    }
                }
            }
        }
    });
});
Zuul
  • 16,217
  • 6
  • 61
  • 88
1

You can use this function:

function validateEmail(email) {
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if( !emailReg.test( email ) ) {
        return false;
    } else {
        return true;
    }
}
Jeroen
  • 13,056
  • 4
  • 42
  • 63