Bicycle Dave
Bicycle Dave

Reputation: 484

regular expression for asp.net validation control

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.

Upvotes: 0

Views: 1314

Answers (3)

Dilip Suvagiya
Dilip Suvagiya

Reputation: 394

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.

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

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"/>

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

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.

Upvotes: 3

Related Questions