I have a field on an MVC 4 View which requires regular expression validation. I'm trying to limit entry to characters 'A' through 'Z' (case insensitive) only.
My C# property looks as follows:
/// <summary>
/// Gets or sets the Revision property
/// </summary>
[DataMember]
[Required]
[RegularExpression("(/^[a-z]+$/i)", ErrorMessage = "Please enter a character between 'A' and 'Z'")]
public string Revision { get; set; }
The generated HTML looks as follows:
<div class="formEditControl">
<input Style="width:30px;" data-val="true" data-val-regex="Please enter a character between 'A' and 'Z'" data-val-regex-pattern="(/^[a-z]+$/i)" data-val-required="The Revision field is required." id="Revision" name="Revision" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Revision" data-valmsg-replace="true"></span>
</div>
But when I execute it, I get the following error on valid data entry:
I don't have a great deal of experience with MVC 4 or RegEx, so it's not clear to me why this isn't working.
You're regular expression is only allowing lowercase characters. Therefore when you input a 'D' it responded with the error. For all letters, case insensitive, your regex should look like this ^[a-zA-Z]*$
. Try that and it should work.
你有(/
开头,但这会产生分隔符()
。我不确定你是否甚至需要分隔符。删除(
和)
。如果这不起作用,请使用
^[a-zA-Z]+$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.