0

In an ASP.NET Listview, I need to raise an error with a RegularExpressionValidator control if a string in an textbox control exceeds a given length. I have minimal experience with regular expressions and hopefully someone can tell me if this is a good use of regular expressions and help me get started with the expression. Thanks.

3 Answers 3

3

You don't need validator to do that. Just set TextBox.MaxLength property to your desired length.

TextBox.MaxLength Property

Gets or sets the maximum number of characters allowed in the text box.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much - exactly what I needed even if I feel pretty dumb for missing such a simple solution. Live and Learn! Thanks again.
0

As you said, use a Regular Expression Validator and set the expression to something like this:

^([\S\s]{0,10})$

Replace the 4 with your desired max lenght.

Update:

<asp:TextBox id="wtxtTPP" Runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" 
        ErrorMessage="RegularExpressionValidator" 
        ValidationExpression="^([\S\s]{0,0})$" 
        ControlToValidate="wtxtTPP" />

Or

Use MaxLength Property

like

 <asp:TextBox id="wtxtTPP" Runat="server" MaxLength="10"/>

2 Comments

It's worth noting that if someone disables or modifies MaxLength using Firebug (for example), they can submit the form with more than 4 characters in your textbox. You will need the RegularExpressionValidator (or a server-side validation approach) to ensure the value is not greater than 4 characters.
Thanks Vignesh, your RegExp example works perfectly and has the advantage of giving a normal validation error message. Both the these methods together works very well - along with the server side.
0

Some of the properties of regular expression:

<asp:RegularExpressionValidator
AccessKey="string"
AssociatedControlID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
    Inset|Outset"
BorderWidth="size"
ControlToValidate="string"
CssClass="string"
Display="None|Static|Dynamic"
EnableClientScript="True|False"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
ErrorMessage="string"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
            Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SetFocusOnError="True|False"
SkinID="string"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
ValidationExpression="string"
ValidationGroup="string"
Visible="True|False"
Width="size"
/>

You can refer to MSDN docs for RegularExpressionValidator for more detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.