Reputation: 283
I have applied client side validation using RegularExpression Annotation in ViewModel.
The validation is GroupName should startwith either TPDC_ or DMK_ with atleast one more letter
[Required(ErrorMessage = "Group Name is required")]
[RegularExpression("^(TPDC_|DMK_)+", ErrorMessage = "Group Name should begin with TPDC_ or DMK_")]
public string GroupName { get; set; }
But the regex used is not working as expected.
Upvotes: 1
Views: 47
Reputation: 626748
If you plan to match a string that starts with TPDC
or DMK
and then has a _
followed with one or more letters, you may consider using
^(TPDC|DMK)_[A-Za-z]+$
Or, if after _
, there may be any characters, but a letter is required to appear somewhere, you may use
^(TPDC|DMK)_[^A-Za-z]*[A-Za-z].*
See regex demo #1 and regex demo #2.
Details
^
- start of string(TPDC|DMK)
- either TPDC
or DMK
_
- an underscore[A-Za-z]+
- 1+ ASCII letters (\p{L}+
will only work on the server side!)[^A-Za-z]*[A-Za-z].*
- any 0 or more chars other than ASCII letters (\P{L}*
is the Unicode equivalent of [^A-Za-z]*
that will only work on the server side), an ASCII letter, and then any 0 or more chars other than line break chars, as many as possible.$
- end of string.Upvotes: 1
Reputation: 163277
You don't need to repeat the group with the alternation. If you want the following letter to be A-Z you could add a character class after it. The underscore might be moved to after closing the group.
^(?:TPDC|DMK)_[A-Z]
Explanation
^
Start of string(?:
Non caputure group
TPDC|DMK
Match TPDC or DMK)
Close group_[A-Z]
Match an underscore followed by a char A-ZOr if you want to allow any letter from any language you could use \p{L}
instead
^(?:TPDC|DMK)_\p{L}
Upvotes: 1