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

});
5
  • You're matching the string "#emailAddr", not the value of that element. Commented Jul 9, 2013 at 0:01
  • You need to match against the value, not the element - $('#emailAddr').val() Commented Jul 9, 2013 at 0: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. Commented Jul 9, 2013 at 0:05
  • Thanks guys don't believe i missed something so stupid. Commented Jul 9, 2013 at 0:06
  • possible duplicate of Validate email address in Javascript? Commented Jul 9, 2013 at 4:39

3 Answers 3

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

});
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.