Showing posts with label RegularExpressionValidator. Show all posts
Showing posts with label RegularExpressionValidator. Show all posts

Number Validation in Textbox of ASP.NET Using Regular Expression Validator

Below is a simple example of Number Validation in Textbox of ASP.Net Using Regular Expression Validator. 
It will also allow all decimal number, excluding all alphanumeric characters.

<asp:TextBox ID="AssetsTextBox" runat="server" Width="240px"></asp:TextBox>                               
                                
<asp:RegularExpressionValidator ID="revAssets" runat="server" 
ErrorMessage="Assets must be a number" Font-Bold="False" 
Font-Size="Small" 
ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$" 
ControlToValidate="AssetsTextBox" Display="Dynamic" >
</asp:RegularExpressionValidator>

Validate Email Address using RegularExpressionValidator

Sometimes there might be the requirement to Validate Email Address using RegularExpressionValidator in application.
If Email address Textbox on your form is Required Field, then try to use following code on your page.

<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="txtEmailRequired" runat="server" 
                            ControlToValidate="txtEmail" 
                            Text="* Required" 
                            Display="Dynamic" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                ErrorMessage="eg: emailAdd@domain.com" 
                                ControlToValidate="txtEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                                Display="Dynamic">
</asp:RegularExpressionValidator>                     

If Email address Textbox on your form is NOT a Required Field, then you can use following code on your page.

<asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                ErrorMessage="eg: emailAdd@domain.com" 
                                ControlToValidate="txtEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                                Display="Dynamic">
</asp:RegularExpressionValidator>