Reputation: 2150
I've a textbox for name field where I've used asp validation for proper name format. I want to validate multiple spaces between the strings. How can I do that? The leading and trail spaces are removed by trim() function but how can I validate multiple spaces between the strings? like
multiple spaces
no space
My validation code::
<label>
<span>Full name</span>
<input type="text" id="txt_name" runat="server" required="required"/>
<asp:RegularExpressionValidator ID="rev_txt_name" runat="server" ControlToValidate="txt_name" ForeColor="Red"
ErrorMessage="Invalid name!" SetFocusOnError="True" ValidationExpression="^[a-zA-Z'.\s]{2,50}"></asp:RegularExpressionValidator>
</label>
Upvotes: 1
Views: 476
Reputation: 626747
The pattern you are using allows matching whitespace anywhere inside the string and any occurrences, consecutive or not, since it is part of a rather generic character class. You need to use a grouping and quantify it accordingly:
^(?=.{2,50}$)[a-zA-Z'.]+(?:\s[a-zA-Z'.]+)*$
Note that the (?=.{2,50}$)
lookahead requires the whole line to be of 2 to 50 chars long.
See the regex demo.
Details:
^
- start of string(?=.{2,50}$)
- a positive lookahead requiring any 2 to 50 chars other than a newline up to the end of the string[a-zA-Z'.]+
- 1+ letters, single quote or dot chars(?:
- a non-capturing group start:
\s
- 1 whitespace[a-zA-Z'.]+
- 1+ letters, single quote or dot chars)*
- zero or more (*
) occurrences$
- end of stringUpvotes: 1