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

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

我有 asp.net mvc5 应用程序,我正在尝试验证我在 c# 模型 class 中定义的正则表达式。当用户填写表单时,我需要对单个输入字段进行客户端验证。我在在线网站上测试了表达,它是正确的。不管我在现场说什么,它都会变成红色并显示错误消息。

型号

[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输出

<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

 $(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);
    });

});

我的 javaScript 在部分视图中

我找到了如下答案;

 [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);
});