I want my validator to not allowing the user to enter more then 1 blank space since my application will crash. And a maximum of 25 characters, what is the regex for this?
<asp:RegularExpressionValidator ValidationGroup="grpSearch" ID="valSearch" ControlToValidate="txtSearchFor" ValidationExpression="^[a-zA-Z0-9][a-zA-Z0-9 ]+$" runat="server" ForeColor="Red"/>
The expression I have tried there does not work.
You may use
ValidationExpression="^(?!.{26})[a-zA-Z0-9]+( [a-zA-Z0-9]*)?$"
See the regex demo .
Details :
^
- start of string (?!.{26})
- no 26 chars allowed (25 and fewer only) [a-zA-Z0-9]+
- 1+ alphanumeric chars ( [a-zA-Z0-9]*)?
- optional group matching a space and 0+ alphanumeric chars $
- end of string Just in case it is of interest: to disallow the space at the end of string, the *
quantifier ( zero or more occurrences) should be replaced with the +
quantifier ( one or more occurrences).
Try this:
^[^ ]+ [^ ]+$
This is of a similar theme, but asserts your requirement for alpha-numeric characters only:
^(A-Za-z0-9|[^ ])+ (A-Za-z0-9|[^ ])+$
Tested here: https://regex101.com/r/3w6B6h/3
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.