我在这样的模型中使用了 Datannoation 验证
[RegularExpression(@"^([a-zA-Z][\w.]+|[0-9][0-9_.]*[a-zA-Z]+[\w.]*)$", ErrorMessage = "Invalid username")]
[Required(ErrorMessage = "User name is required")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[Display(Name = "User Name")]
public string UserName { get; set; }
在视图中
<p>
@Html.TextBoxFor(m => m.UserName, new {@id="UserNamereg", @Class = "span5", @placeholder = "UserName",onchange = "CheckRegUserAvailibility(this,'/Account/CheckUserName');"})
</p>
此外,我想检查用户名是否已经存在于数据库中,为此我正在使用 jquery。
function CheckAvailibility(thisElm, targeturl, avilibilityfieldname) {
var stridvalue = $(thisElm).val();
var strid = $(thisElm).attr("id");
var data = $("#" + strid).serialize();
var errorMsg = "The " + avilibilityfieldname.toLowerCase() + " '" + stridvalue + "' is not available. Try another " + avilibilityfieldname.toLowerCase() + "."
if (stridvalue != "") {
$.post(targeturl, data, function (responseResult) {
if (responseResult == "false") {
$("#" + strid).removeClass("input-validation-valid").addClass("input-validation-error");
$("#" + strid).siblings("span").removeClass("field-validation-valid").addClass("field-validation-error");
$("#" + strid).siblings("span").empty().html(errorMsg);
$("#" + strid).val("");
} else if (responseResult == "true") {
$("#" + strid).removeClass("input-validation-error").addClass("input-validation-valid");
$("#" + strid).siblings("span").removeClass("field-validation-error").addClass("field-validation-valid");
}
});
}
}
问题是只有 jquery 验证在客户端工作,因为我正在使用“onchnge”事件。有什么方法可以使两个验证都正常工作吗?谢谢帮助