开发者

how can i display form errors in div with jquery

i know this is very simple question but i am new on this so please help me. i want to display the error message on my contact_result div but the script is not working, i wrote the following code:

$("document").re开发者_运维百科ady(function() {
$("#contact_submit").click(function() {
    var first_name = $("#first_name").val();
    var last_name = $("#last_name").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var atpos = email.indexOf("@");
    var dotpos = email.lastIndexOf(".");
    var msg = "";

    if(first_name == "") {
        msg+= "First Name is required.<br />";
    }

    if(last_name == "") {
        msg+= "Last Name is required.<br />";
    }

    if(Phone == "") {
        msg+= "Phone Number is required.<br />";
    }

    if (email == "" || atpos < 1 || dotpos < atpos+2 || dotpos+2 > email.length) {
    msg+= "Please enter a valid Email address. <br />";
    }

    if(msg !== "") {
        $("#contact_result").html(msg).show();
    }else {
        // post valuses
    }
});
});


try

if(phone == "") {

not

if(Phone == "") {


Notice the error:

var phone = $("#phone").val();

your variable is "phone",but you use "Phone",this is a error, Javascript identifiers are case-sensitive.

The error can't display in div, because it you use use it like

this:$("#contact_result").html(msg)" not "$("#contact_result").html(msg).show()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜