分享
是一种美德

jq validate 验证优化

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

<!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>

项目详细介绍地址

分享按钮
赞(2)
转载请注明来源地址:MN乐享 » jq validate 验证优化

评论 抢沙发

评论前必须登录!