1

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;
    } 

});
Stephen Romero
  • 25
  • 1
  • 2
  • 6
  • You're matching the string "#emailAddr", not the value of that element. – Adrian Wragg Jul 09 '13 at 00:01
  • You need to match against the value, not the element - `$('#emailAddr').val()` – jterry Jul 09 '13 at 00:02
  • validating emails with regex will almost always fail as there are too many variations, special characters etc. Check if `@` and `.` is present, and filter out stuff like `<` and `>` and leave it at that. – adeneo Jul 09 '13 at 00:05
  • Thanks guys don't believe i missed something so stupid. – Stephen Romero Jul 09 '13 at 00:06
  • possible duplicate of [Validate email address in Javascript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Ro Yo Mi Jul 09 '13 at 04:39

3 Answers3

3

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;
    } 

});
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
0

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

Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
0

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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
ArmyAngel
  • 23
  • 1
  • 6