Validation是jQuery的插件,提供的方法可以大大简化验证表单的工作,功能也足够一般的需要了。rules也足够简单,很容易上手,举个简单的例子,用validation来验证注册表单。 首先确定验证的目标是: 1. 必填项不能为空 2. 注册用户名必须为6-12个字符内 3. 合格的email格式 4. 密码必须为6-18个字符 5. 确认密码必须跟密码一致 ok,目标很明确了。正片开始 复制代码 代码如下: <BR>$(function(){ <BR>$( "#regForm" ).validate({ <BR>rules: { <BR>// 注册用户名 <BR>username: { <BR>required: true, <BR>minlength: 5, <BR>maxlength: 12 <BR>}, <BR>// email <BR>email: { <BR>required: true, <BR>email: true <BR>}, <BR>// 密码 <BR>password: { <BR>required: true, <BR>minlength: 6, <BR>maxlength: 18 <BR>}, <BR>// 确认密码 <BR>confirm_password: { <BR>equalTo:"#password" <BR>}, <BR>// 检验验证码 <BR>captcha: { <BR>required: true, <BR>remote: "checkCaptcha.php" <BR>} <BR>}, <BR>messages: { <BR>// 注册用户名 <BR>username: { <BR>required: "此项不能为空", <BR>minlength: "不能少于5个字符", <BR>maxlength: "不能多于12个字符" <BR>}, <BR>// email <BR>email: { <BR>required: "此项不能为空", <BR>email: "email格式不正确" <BR>}, <BR>// 密码 <BR>password: { <BR>required: "此项不能为空", <BR>minlength: "不能少于6个字符", <BR>maxlength: "不能多于18个字符" <BR>}, <BR>// 确认密码 <BR>confirm_password: "两次输入密码不一致", <BR>// 检验验证码 <BR>captcha: { <BR>required: "请输入验证码", <BR>remote: "验证码输入错误" <BR>} <BR>} <BR>}); <BR>}); <BR> 运行后,得到下面的结果 更多用法参考 http://www.jb51.net/article/24405.htm