ASP Regular expression to check length of textbox value
Hi, here is a regular expression validator in ASP which you can use to check if a certain textbox has less than a certain number of characters.
<asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{1,<n>}" errormessage="Please note that there should be less than <n> number of characters in the textbox"> </asp:regularexpressionvalidator>
So, if you intend to make the textbox accept a number of characters in a range(say between 5 to 10) then the following changes would work
<asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{5,10}" errormessage="Please note that there should be between 5 to 10 characters in the textbox"</asp:regularexpressionvalidator>
Suppose you need to limit the characters to only alphanumerics, then use the following regular expression for the validator.
"[A-Za-z0-9]{5,10}"
Now, suppose you only wish to have a certain number of special characters only. Say you need to accept the space( ) period(.) , comma(,) and dash(-) only with the alphanumerics. Then use the following regex.
"[A-Za-z0-9.-,\s]{5,10}"
You can modify the above code for your custom needs.
Have fun.
<asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{1,<n>}" errormessage="Please note that there should be less than <n> number of characters in the textbox"> </asp:regularexpressionvalidator>
So, if you intend to make the textbox accept a number of characters in a range(say between 5 to 10) then the following changes would work
<asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{5,10}" errormessage="Please note that there should be between 5 to 10 characters in the textbox"</asp:regularexpressionvalidator>
Suppose you need to limit the characters to only alphanumerics, then use the following regular expression for the validator.
"[A-Za-z0-9]{5,10}"
Now, suppose you only wish to have a certain number of special characters only. Say you need to accept the space( ) period(.) , comma(,) and dash(-) only with the alphanumerics. Then use the following regex.
"[A-Za-z0-9.-,\s]{5,10}"
You can modify the above code for your custom needs.
Have fun.
Comments