Javascript 在jQuery中进行验证,并重定向带有错误字段的新页面
如何使用jquery验证我的所有字段 如果验证失败,我想重定向到新页面并列出所有验证失败的字段。如果成功,我将执行插入操作Javascript 在jQuery中进行验证,并重定向带有错误字段的新页面,javascript,jquery,asp.net-mvc-3,validation,Javascript,Jquery,Asp.net Mvc 3,Validation,如何使用jquery验证我的所有字段 如果验证失败,我想重定向到新页面并列出所有验证失败的字段。如果成功,我将执行插入操作 Example <input class="textbox validate"type="text"> <input class="textbox validate"type="text"> //validate the all the field with having "validate" class $(".validate").e
Example
<input class="textbox validate"type="text">
<input class="textbox validate"type="text">
//validate the all the field with having "validate" class
$(".validate").each
示例
//使用“validate”类验证所有字段
$(“.validate”)。每个
我正在使用MVC-3,但我想在自定义j查询逻辑中使用。我是j-query的新人
提前谢谢 假设您有一个用于验证的函数:
function validate (text) {
...
return true; //or false
}
然后你可以做一件事:
var validationErrors = [],
errorPageURL = "BASE URL for your error page";
$(".validate").each(function (index, element) {
if (!validate(element.val()) {
validationErrors.push($(element).id);
}
});
if (validationErrors.length === 0) {
//Do your input magic
} else {
window.location.replace(errorPageURL + "?errors=" + encodeURI(JSON.stringify(validationErrors)));
}
一些参考链接: