[转]MVC+JQuery validate已毕用户输入验证

http://docs.jquery.com/Plugins/Validation/validate

 

 

 

<script src=”/Js/jquery-1.4.2.js” type=”text/javascript”></script>

<label id=”messageBox”></label>

2.在View视图某一个地点停放

MVC服务器端:

要匡助regex形式的印证,请在jquery.validate.js插足:

 

 

其余常用的求证措施有:

 

1.引放相应的JS文件

 

 $(function() {
        $("#form1").validate({
            rules: {
                LoginName: { required: true, regex: "^[0-9]+$" }
            },
            messages: {
                LoginName: "<%=Resource.LoginName + Resource.WordSpace + Resource.CanNotBeBlank%>"
            },
            errorLabelContainer: "#messageBox",
            wrapper: "li"
        });
    });

如上代码已落到实处双语化提醒

required, remote, minlength, maxlength, rangelength, min, max, range,
email, url, date, dateISO, number, digits, creditcard, accept, equalTo等

 

 4.以Ajax情势提交到服务器的,JS代码要改为:

var validate = null;
var opts = {
    rules: {
        LoginName: { required: true, regex: "^[a-zA-Z][a-zA-Z0-9._-]{3,20}$" }
    },
    messages: {
        LoginName: "请输入正确的登陆名"
    },
    errorLabelContainer: "#messageBox",
    wrapper: "li"
};

function checkForm() {
    var b = validate.checkForm();
    validate.showErrors();
    return b;
}

$(function () {
    validate = $("#form1").validate(opts);
});

function SaveUser() {
    if (!checkForm()) {
        return;
    }
    //...........
}
// 正则表达式
$.validator.addMethod(
    "regex",
    function (value, element, regexp) {
        var check = false;
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
);

JS客户端:

3.以健康的submit()格局提交,在页面最上面进入以下JS代码

<script src=”/Js/jquery.validate.js” type=”text/javascript”></script>

 

本文转自:http://www.cnblogs.com/ahui/archive/2010/10/08/1845677.html

 

 

 

ModelState.AddModelError(“LoginName”, Resource.LoginName + Resource.WordSpace + Resource.CanNotBeBlank);

1.在controller中注脚用户输入,借使证实失利,执行

<%=Html.ValidationSummary()%>

 

可参见:

 

2.在View视图某一个地方放置

 

 

 

 

 

相关文章