jq validate 验证优化

之前使用validate比较简单,并没有做非常大的优化,现在整理了一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery表单验证</title>
<script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<link href="css/index.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("#FormTo").validate({
rules: {
uname: "required",
pwd: {
required: true,
rangelength: [4, 20]
}
},
messages: {
uname: "用户名不能为空",
pwd: {
required: "密码不能为空!",
rangelength: "密码最少4个字符,最多20个字符!"
}
},
submitHandler: function (form) {
$(form).ajaxSubmit({
url: $(form).attr("action"),
type: 'post',
dataType: 'json',
success: function (data) {
if (data.success) {
alert(data.msg);
window.location.href = "Default.aspx";
} else {
alert("出现未知错误");
}
}
});
},
highlight: function (element, errorClass, validClass) { //element出错时触发
$(element).addClass("inputerror").next().next().show();
},
unhighlight: function (element, errorClass, validClass) { //通过的元素加样式
$(element).removeClass("inputerror").next().html("通过").next().hide()
},
debug: true,
errorClass: "error",
errorElement: "span",
success: "correct"
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="titleUI"> <h3>用户注册资料</h3></div>
<form id="FormTo" class="democss" action="FormRequest.ashx" method="post">
  <fieldset><legend>用户注册表单</legend>
<div class="formUI">
<p>
<label for="name">用户名:</label><input type="text" class="inputclass w100" id="uname" name="uname" /><span class="msg">用户名必须填写</span>
</p>
<p>
<label for="name">密码:</label><input type="password" class="inputclass w200" name="pwd" /><span class="msg">密码4-20个字符之间</span>
</p>
<p>
<label for="name">确认密码:</label><input type="password" class="inputclass w200" name="pwdTo" /><span class="msg">密码4-20个字符之dfsafdasdfasdfsaf间</span>
</p>
<p>
<label for="name">真实姓名:</label><input type="text" class="inputclass w200" name="realname" /><span class="msg">真实姓名必须为字母或汉字 可空</span>
</p>
<p>
<label for="name">新闻内容:</label><textarea rows="5" cols="5" name="content" class="textareaclass w500"></textarea>
</p>
<p>
<label for="name">是否推荐:</label>推荐:<input type="checkbox" name="Top" value="0" /> 特价:<input type="checkbox" name="Top" value="1" /> 新品:<input type="checkbox" name="Top" value="2" />
</p>
<p>
<label for="name">产品分类:</label><select name="category" class="selectUI" >
<option value="1">我们的产品</option>
<option value="3">客户的产品</option>
<option value="2">他们的产品</option>
</select>
</p>
<p>
<label for="name"> </label><input type="submit" class="submitClass" value="添加" />
</p>
<div class="clear"></div>
</div>
</fieldset>
  </form>
<div id="messageBox"> </div>
</div>
</body>
</html>

项目详细介绍地址
https://github.com/twang211/webdemo/tree/master/jqueryvalidate%E9%AA%8C%E8%AF%81%E8%A1%A8%E5%8D%95%E6%A1%86%E6%9E%B6

Mr.Naas wechat
欢迎关注本人公众号