Checking the format of the contact email address

I use a simple form with a name, email and comment field to send messages from a web page. There is also a hidden header field, which must be empty for the form to be submitted - spam protection if you want.

JQuery code I run the form before submitting the works in order, but currently only searches for the "@" character in the email address field. I want to better check for a correctly formatted email address.

Here is the code.

    $(function() {  
    $('.error').hide();  
    $(".button").click(function() {  
    // validate and process form here  

        $('.error').hide();  
        var name = $("input#name").val();  
            if (name == "") {  
            $("label#name_error").show();  
            $("input#name").focus();  
            return false;
        }  
        var email = $("input#email").val();  
            if (!(email.indexOf('@') > 0)) {
            $("label#email2_error").show();  
            $("input#email").focus();  
        return false;  
        }  
        var message = $("textarea#message").val();  
            if (message == "") {  
            $("label#message_error").show();  
            $("textarea#message").focus();  
        return false;  
        } 
        var title = $("input#title").val()
            if(title !== "") {  
            $("input#title").focus();  
        return false;  
        } 

    var dataString = 'name='+ name + '&email=' + email + '&message=' + message;  
    //alert (dataString);return false;  
    $.ajax({  
        type: "POST",  
        url: "sendmail.php",  
        data: dataString,  
        success: function() {  
            $('#message_form').html("<div id='response'></div>");  
            $('#response').html("<div id='content_success_block' class='shadow_box'>")  
                .append("<div id='success_image'><img src='assets/misc/success.png'></div>")
                .append("<div id='success_text'>Thanks for contacting us! We will be in touch soon.</div>")
                .append("</div>")                   
            .hide()  
            .fadeIn(2500, function() {  
                $('#response');  
            }); 
        }  
    });  
    return false; 
    });

});

Any help is appreciated.

+5
source share
4 answers

The one that worked best for "me"

function validEmail(v) {
    var r = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
    return (v.match(r) == null) ? false : true;
}

Important!

. Regex , ", ". , , .


Incorperated

}  
var email = $("input#email").val();  
if (!validEmail(email)) {
    $("label#email2_error").show();  
    $("input#email").focus();  
    return false;  
}  

, : - ( , )

+16

Blur , , chk .

 function validateForm()
{
 var x = document.getElementById('txtEmail');
 var atpos=x.value.indexOf("@");
 var dotpos=x.value.lastIndexOf(".");
 if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
    alert("Not a valid e-mail address");
    return false;
  }
}
+2

, true false, . regExp.test(value)

 var emailIsValid =  function(value){
        var regex = new RegExp ("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$");
        return regex.test(value);
    }

var email = $("input#email").val();  
        if (!emailIsValid(email)) {
           $("label#email2_error").show();  
           $("input#email").focus();  
        return false;  
        }  
+1

The problem with the Regex above (SpYk3HH) is that it checks somthing like: .me @ gmail.c I think the best way is to use some ajax to call the PHP function (if you really use PHP):

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
echo "Email Not Valid ! ";
-1
source

All Articles