简体   繁体   English

在客户端验证中,asp.net mvc5中的正则表达式不适用于名称

[英]Regular expression not working for name in asp.net mvc5 on client-side validation

I have asp.net mvc5 app and I am trying to validate regular expression that I have define in c# model class. 我有asp.net mvc5应用程序,正在尝试验证在c#模型类中定义的正则表达式。 I need to do client side validation for individual input field as user goes along filling form. 当用户填写表格时,我需要对各个输入字段进行客户端验证。 I have test expression on online site and it correct. 我在在线站点上有测试表达式,它是正确的。 Doesn't matter whatever I say in field, it turn to red and showing error message. 不管我在现场说什么,它都变成红色并显示错误消息。

model 模型

[StringLength(50)]
[Required(ErrorMessage = "Required Title")]
[RegularExpression("(/^[a-z ,.'-]+$/i)", ErrorMessage = "Only Allowed Alphabet Character In Title"
[Display(Name = "Title")]
public string Title { get; set; }

html out-put HTML输出

<input class="form-control text-box single-line input-validation-error" data-val="true" data-val-length="The field Title must be a string with a maximum length of 50." data-val-length-max="50" data-val-regex="Only Allowed Alphabet Character In Title" data-val-regex-pattern="(/^[a-z ,.'-]+$/i)" data-val-required="Required Title" id="Title" name="Title" value="" type="text">

JavaScript 的JavaScript

 $(function () {

    jQuery.validator.unobtrusive.parse();

    jQuery.validator.unobtrusive.parse("#CreateStudentProfileForm");


});



$(document).ready(function () {

    $("#CreateStudentProfileForm").find("input,textarea,select").on('input', function () {

        var _currentElement = $(this).attr('id');

        var v = $("#CreateStudentProfileForm").validate().element("#" + _currentElement);

        alert("va   " + v);
    });

});

my javaScript inside the partial-view 我的javaScript局部视图内

I have found the anaswer as below; 我发现以下问题:

 [MaxLength(50)]
 [Required(ErrorMessage = "Required Title")]
 [RegularExpression("(^[a-z ,.'-]+$)", ErrorMessage = "Only Allowed Alphabet Character In Title")]
 [Display(Name = "Title")]
 public string Title { get; set; }

... ...

 $("#CreateStudentProfileForm").find("input,textarea,select").on('input', function () {

        var _currentElement = $(this).attr('id');

        var v = $("#CreateStudentProfileForm").validate().element("#" + _currentElement);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM