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

Monday, 8 October 2012

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.

Thursday, 4 October 2012

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

Thursday, 23 August 2012

 Click Here to Download TextBoxMaximumLengthApplication.zip

There is always a need of application to set maximum length allowed in textbox.
In this article we demonstrate both single line textbox and multi line textbox max length validation. 
There is a different way to set maximum characters allowed for single line textbox and multiline textbox.
You can set "MaxLength" property for single line textbox.

 For multiline textbox "MaxLength" does not supported.

So for Multi Line textbox we need some tricky approch. We can use javascript , JQuery or server side checking. Here we are going to use .Net Validator controls. We are using "RegularExpressionValidator" control.

Here is example for this.

In this example we are taking two textbox one is for ProdutTitle which is singleline textbox and other is for ProductDescription which is MultiLine textbox. For ProductTitle we are using MaxLength property to set maximum allowed characters. Which is 20 characters.
For ProductDescription we are using "RegularExpressionValidator" control to set maximum allowed characters. Which is 50 characters.
We are using Regex expression for validation.
The "RegularExpressionValidator" control display error message after typing in textbox focus or cursor moved out from textbox.
You can also make server side validation check for regular expression validation using "Page.Validate()" Method and "Page.IsValid" property.

In output you can see that if more than maximum allowed characters type in textbox it will give error message. You can also make you customize message.


ASPX Code:
        <table>
            <tr>
                <td>
                    Product Title :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductTitle" MaxLength="20" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductDescription"  TextMode="MultiLine"></asp:TextBox>
                    <asp:RegularExpressionValidator runat="server" ID="REVProductDescription" ControlToValidate="txtProductDescription"
                        ValidationExpression="^[\s\S]{0,50}$" style="color:Red;font-weight:bold" ErrorMessage="Please enter a maximum of 50 characters"
                        Display="Dynamic"></asp:RegularExpressionValidator>
                </td>
            </tr>
        </table>

Output : 
(To view original image , click on image)

For Beginning .Net articles. Click Here...
 
Here is very useful .Net Tips.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.