Jquery验证可以通过点击按钮来触发。onclick事件可以有两种方式触发验证,1. var ret=$("#form1").valid(); 2.ret=add_valid().form();
但是不知道为什么在我的.Net MVC程序里不能触发,实在找不到原因。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
<script>
function add_valid()
{
return $("#add_modalForm").validate({
errorElement: "span",
messages: {
add_userName: {
required: " (必需字段)",
minlength: " (不能少于 3 个字母)"
},
add_password: {
required: " (必需字段)",
minlength: " (字母不能少于 5 个且不能大于 12 个)",
maxlength: " (字母不能少于 5 个且不能大于 12 个)"
}
}
});
}
</script>
<style>
.error{
color:red;
}
</style>
</head>
<body>
<form method="POST" id="add_modalForm">
<fieldset>
<legend>登录框</legend>
<p>
<label for="user">用户名</label>
<input id="add_userName" name="user" required>
</p>
<p>
<label for="password">密码</label>
<input id="add_password" type="password" name="password" required>
</p>
<p>
<button type="button" onclick="(function(){ var t=$(‘#add_modalForm‘).valid();if(t) alert(‘验证通过‘); else alert(‘验证未通过‘); })()">login</button>
</p>
</fieldset>
</form>
</body>
</html>