Stephen Romero
Stephen Romero

Reputation: 25

Jquery validating email using RegEx

I'm trying to validate a email using the code below currently it tests the email to see if it matches the regex but even when a valid email is entered the error is activated and the page does not submit

    $('#emailsubmit').submit(function() {
    var email = new RegExp(/^(([^<>()[\]\\.,;:\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,}))$/);


    if (!("#emailAddr").match(email)) {
        $("#errormsg").html("Please insert a valid email");
        $("#errormsg").show();

         var emailaddr = $("#emailAddr").val();

        alert(emailaddr);
        return false;
    } 

});

Upvotes: 1

Views: 11804

Answers (3)

ArmyAngel
ArmyAngel

Reputation: 23

This is my script, that I've used for a while... It works great and also checks zip code and other form fields for input.

function checkEmail(e_mail) {
  var str = new String(e_mail);
  var biz_name = document.myForm.biz_name;
  var zip_code = document.myForm.zip_code;
  var e_mail = document.myForm.e_mail;
  var str2 = new String(zip_code);
  var terms = document.myForm.terms;

      if (biz_name.value == "")
    {
        window.alert("Oops. Please enter your business name!");
        biz_name.focus();
        return false;
    }
    if (zip_code.value == "")
    {
        window.alert("Oops. Please enter your business zip code!");
        zip_code.focus();
        return false;
    }
    if (e_mail.value == "")
    {
        window.alert("Oops. Please enter your email address!");
        e_mail.focus();
        return false;
    }
    if (terms.checked == false)
    {
        window.alert("Oops. Please agree to the terms and conditions!");
        terms.focus();
        return false;
    }
  var isOK = true;

  rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
  if( rExp.test(str) )
    isOK = false;
  if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
    isOK = false;
  if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
  if( str.slice(0,str.indexOf('@')).length < 1 )
    isOK = false;
  if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;
  if( !isOK )
    alert( "Oops! A valid email is needed.  Check it and try again!" );
    e_mail.focus();

  return isOK;
}

Upvotes: 0

Daniel
Daniel

Reputation: 18672

You forgot to use JQuery's .val() method which gets value of an element. Instead you were trying to use .match() on HTML element(which is obviously not a string). Code:

    $('#emailsubmit').submit(function() {
    var email = new RegExp(/^(([^<>()[\]\\.,;:\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,}))$/);


    if (!("#emailAddr").val().match(email)) {
        $("#errormsg").html("Please insert a valid email");
        $("#errormsg").show();

         var emailaddr = $("#emailAddr").val();

        alert(emailaddr);
        return false;
    } 

});

Upvotes: 3

Adrian Wragg
Adrian Wragg

Reputation: 7401

You are matching the value "#emailAddr", not the value of the element with ID "emailAddr".

Upvotes: 0

Related Questions