Wednesday, December 23, 2009

Data type validation using validation control

While developing ASP.NET application you would have come across scenarios where you would like to validate whether the value entered is of a particular data type. The options available with an ASP.NET developer is to write javascript functions to check whether the entered value complies with a data type or make the page postback and check the data type in server side. These two options are time consuming. Is there any simple way of doing this? The answer is to the question is “YES”. One can make use of compare validator to accomplish the task very easily. Lets see how.

To compare whether the entered value is of a particular data type one should make sure that the “Operator” property of compare validator should be “DataTypeCheck” and the “Type” property should be the data type you want to check. These two things are the only requirement to validate the data type. Compare validator supports validation for Currency, Date, Double, Integer and String data type. Below is a sample code which tries to check all these data types.

Enter string: <asp:TextBox ID="txtString" runat="server"></asp:TextBox>
<asp:CompareValidator ID="cmpVldString" runat="server" ErrorMessage="Not a valid string." ControlToValidate="txtString"
Type="String" Operator="DataTypeCheck"> </asp:CompareValidator>
<br />
Enter integer: <asp:TextBox ID="txtInt" runat="server"> </asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Not a valid Integer." ControlToValidate="txtInt"
Type="Integer" Operator="DataTypeCheck"> </asp:CompareValidator>
<br />
Enter double: <asp:TextBox ID="txtDouble" runat="server"> </asp:TextBox>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Not a valid double." ControlToValidate="txtDouble"
Type="Double" Operator="DataTypeCheck"> </asp:CompareValidator>
<br />
Enter date: <asp:TextBox ID="txtDate" runat="server"> </asp:TextBox>
<asp:CompareValidator ID="cmpVldDate" runat="server" ErrorMessage="Not a valid date." ControlToValidate="txtDate"
Type="Date" Operator="DataTypeCheck" > </asp:CompareValidator>
<br />
Enter currency: <asp:TextBox ID="txtCurrency" runat="server"> </asp:TextBox>
<asp:CompareValidator ID="cmpVldCurrency" runat="server" ErrorMessage="Not a valid currencys." ControlToValidate="txtCurrency" Type="Currency" Operator="DataTypeCheck" > </asp:CompareValidator><br />
<asp:Button ID="Button1" runat="server" Text="Button" />

In the above code sample you can see that I am checking the datatype for string, Integer, date, double and finally currency. Isn’t it so easy to check the data type?

Now since we are validating date you would like to check against a particular format say “DD/MM/YYYY”. The default date format against which the compare validator checks the date is “YYYY/MM/DD”. One way specify the date format is to assign the page’ culture to the required culture which supports your date format. Say we want to check “DD/MM/YYYY” then we can set the following in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    Page.Culture = "hi-IN";

In the above code we are setting the culture for the page as Hindi-India which will accept the date in “DD/MM'/YYYY” format. Another way specifying the format in compare validator is to assign culture to the control which is specified in “ControlToValidate” property of the compare validator. For e.g. you have date picker assigned to a compare validator then assigning the culture or date format to the date picker will do the trick. As such there is no way to specify date format or culture to a compare validator.

Note: When you use compare validator to check currency data type keep in mind it doesn’t check for currency symbol compliance.

Try to know more…..

Sandeep

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It is not working for String

    Enter string:

    ReplyDelete

Please provide your valuable comments.