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

Friday, March 30, 2012

Asp.Net - TextArea validation using RegularExpressionValidator

The Asp.Net TextArea control doesnot support the MaxLength property, though you set the MaxLength property, the TextArea control will not restrict the users to enter text within the set limit. The MaxLengh property works fine with the TextBox controls, but when it comes to TextArea controls (TextMode = MultiLine) the MaxLength property fails.

The RegularExpressionValidator, control can be used to validate the MaxLength property of the TextArea control. Her’s how this can be accomplished.

Add a TextBox control to your Asp.net form and set its TextMode property to MultiLine, then add a RegularExpressionValidator control with the regular expression to restrict the length of the text in the TextBox.

Here’s the code

<asp:TextBox id="txtProjectDescription"
runat="server"
CssClass="text"
MaxLength="2500"
Width="500px"
TextMode="MultiLine"
Rows="5">
asp:TextBox>

<asp:RegularExpressionValidator
ID="valtxtProjectDescription"
runat="server"
ControlToValidate="txtProjectDescription"
Display="None"
ErrorMessage="Project Description Should be less than 2500 Characters"
SetFocusOnError="True"
ValidationExpression="^([\S\s]{0,2500})$">
asp:RegularExpressionValidator>

Add the Save Button and the Validation Summery control

<asp:Button
ID="cmdSave"
runat="server"
Text="Save"
/>

<asp:ValidationSummary
ID="valSummary"
runat="server"
DisplayMode="List"
ShowMessageBox="True"
ShowSummary="False"
EnableClientScript="true"/>

Now build and run the project, enter a text with length more that 2500 characters in the TextArea. You should get an error message

Project Description Should be less than 2500 Characters

That's it, we have implemented the MaxLength property for a TextArea control in Asp.net

Asp.Net - ValidationSummary using ValidationGroup

In Asp.Net the ValidationSummary control consolidates the validation detials and error messages of all the validation controls in the page and provides a summary report of all the validation failures in the page.

Suppose we have a large page and we need to group the validations in the page based on specific groups, here we need to use the ValidationGroup property, the ValidationGroup property should be set to the controls which are part of the group, the ValidationSummary control and the button which will handle the submit/postback of this group.

Here’s the code below

Asp.Net - Date Validation using RegularExpressionValidator

Date validation using Asp.Net RegularExpressionValidator

Asp.Net offers the following validation controls.

RequiredFieldValidator
RegularExpressionValidator
CompareValidator
CustomValidator
RangeValidator

To validate the date, we can use the RegularExpressionValidator. This validator requires a regular expression which will validate the pattern of data entered in the control.

Open an Asp.net page and add a TextBox control, also add a RegularExpressionValidator control to validate the date entered in the TextBox

<asp:TextBox ID="txtDate" runat="server">asp:TextBox>
<asp:RegularExpressionValidator
ID="val txtDate "
runat="server"
ControlToValidate=" txtDate "
Display="None"
ErrorMessage="Enter a valid Date, Format (mm/dd/yyyy)"
SetFocusOnError="True"
ValidationExpression="^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$">
*asp:RegularExpressionValidator>

Now we have the TextBox Control and a RegularExpressionValidator control to validate the same, we should add a ValidationSummary control to handle all the validations in the page, we shall add the summary control, below the Submit button.

<asp:Button ID="cmdSave" runat="server" Text="Save"/>
<asp:ValidationSummary
ID="valSummary"
runat="server"
DisplayMode="List"
ShowMessageBox="True"
ShowSummary="False"
EnableClientScript="true"/>

Now build and run the project, enter an invalid Date in the textbox and click on the Save button.

You should get the Error message as follows.



Asp.Net - using ValidationControls with ValidationSummary

In Asp.Net we can use the Validation controls which come built in with Asp.net. Asp.Net offers the following validation controls.

RequiredFieldValidator
RegularExpressionValidator
CompareValidator
CustomValidator
RangeValidator

Now suppose we have multiple controls in a form, and each controls is associated with one or more validation controls, we will have to display the summary of all the validation in a single messagebox, to do so we can make use of the ValidationSummary control.

For example let us consider a textbox, which can accept only US Phone numbers, let us try to do the validation for this controls and associate the same with a ValidationSummary control.

Open an Asp.net page and add the control and the validationcontrol

<asp:TextBox
id="txtBusinessOwnerPhone"
runat="server"
CssClass="text"
MaxLength="12">
</asp:TextBox>

<asp:RegularExpressionValidator
ID="valtxtBusinessOwnerPhone"
runat="server"
ControlToValidate="txtBusinessOwnerPhone"
ValidationExpression="[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}[-]"
Display="None"
Text="*"
ErrorMessage="Enter Phone# in the format 999-999-9999">
</asp:RegularExpressionValidator>

Now we have the TextBox Control and a RegularExpressionValidator control to validate the same, we should add a ValidationSummary control to handle all the validations in the page, we shall add the summary control, below the Submit button.

<asp:Button ID="cmdSave" runat="server" Text="Save"/>
<asp:ValidationSummary
ID="valSummary"
runat="server"
DisplayMode="List"
ShowMessageBox="True"
ShowSummary="False"
EnableClientScript="true"/>

Now build and run the project, enter an invalid Phone number in the textbox and click on the Save button.

You should get the Error message as follows.



That's it, we have implemented the ValidationSummary control for the Asp.Net Validation controls.