0

我是 jquery 的新手。我一直在研究并且在尝试使用 query.validate.min.js 加载 ajax 之前尝试验证电子邮件地址时遇到了麻烦。

我想验证表单,并且只有在输入有效时才调用 app.ajax.load。无论我在 ajax 加载之前设置什么条件,表单仍然在未经验证的情况下发布,也忽略了我的回电。(这是一条“谢谢”消息)

有人可以帮我解决这个问题还是我需要编写一个单独的函数来验证?

$('#emailsignup_form').validate({
rules: {
email: {
required: true;
email:true
}
},
messages: {
email: "Invalid Email",
},

submitHandler: function(form){
    app.ajax.load({
         reqName : 'emailSubmit',
         url: '$httpUrl('Bronto-OptIn')$?email=' + $('#email').val(), 
         selector : '#emailbox',
         callback: function(responseText, textStatus) { }
    });
            return false;
});

});


<form id="emailsignup_form" name="emailsignup_form" method="post" action="$httpUrl('Bronto-OptIn', 'fid', 'information')$">
<div class="fl"><input class="email-signup-input" type="text" title="Enter Your Email Address" value="Enter Your Email Address" name="email" id="email" /></div>
<div class="fl"><button class="email-signup-btn" value="Submit" name="submitemail" id="submitemail">Submit</button></div>
</form>
4

1 回答 1

2

当还有一个被调用时,分配一个input nameof对你来说是危险的。从技术上讲,它会起作用,但正如您在下面看到的,它可能会导致很大的混乱。示例:您的内部打算分配给named还是被调用?"email"rule"email"emailmessages:inputemailruleemail


您的rulesmessages...中有一些语法错误

rules: {
email: { 
required: true; // <-- semicolon incorrect, must be a comma
email:true
}
},
messages: {
email: "Invalid Email", // <-- should define a message for each corresponding rule
},

应该是这样的...

    rules: {
        email: { // the field name
            required: true,
            email: true // the rule
        }
    },
    messages: {
        email: { // the field name
            required: "this is required",
            email: "Invalid email" // the rule's message
        }
    },

而且你submitHandler:的格式不正确...

submitHandler: function (form) {
    app.ajax.load({
        reqName: 'emailSubmit',
        url: '$httpUrl('Bronto - OptIn')$?email=' + $('#email').val(),
        selector: '#emailbox',
        callback: function (responseText, textStatus) {}
    });
    return false; 
}); // <-- this is not correct

应该更像这样(假设你的ajax函数是正确的)......

submitHandler: function (form) {
    app.ajax.load({
        reqName: 'emailSubmit',
        url: '$httpUrl('Bronto - OptIn')$?email=' + $('#email').val(),
        selector: '#emailbox',
        callback: function (responseText, textStatus) {}
    });
    return false; 
}

演示:

http://jsfiddle.net/CagLw/

于 2013-01-21T01:49:06.037 回答