Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

ASP.Net: Clientside Date, Double, Integer,Currency Validation

4.00/5 (4 votes)
16 Sep 2010CPOL 33.8K  
When using text field date, Double type (decimal places or different decimal characters in globalization etc) and currency validation on client-side we often use regular expression validator and try to write complex regular expressions.

When using text field date, Double type (decimal places or different decimal characters in globalization etc) and currency validation on client-side we often use regular expression validator and try to write complex regular expressions. The easiest way to validate these data types is to use compare validator. I think it’s the most powerful and awesome control for validating data types. All you have to do is to set

Operator="DataTypeCheck"

And

Type="<<your required type here >>>"

It supports following types:

  • Date
  • Currency
  • Double
  • Integer
  • String

Look at the following examples:

  • This validates the date according to current culture of the application.
    <asp:TextBox ID="txtDateStart" CssClass="input" runat=""server"" Width="200″>
    </asp:TextBox>
    
    <asp:CompareValidator ID="cmprValidatorDateStart" ControlToValidate="txtDateStart" 
        Type="Date" Display="Dynamic" Operator="DataTypeCheck" 
        ErrorMessage="*Not a valid date." runat="server"<>/asp:CompareValidator>
  • This validates double type. The plus point of using this is that for example you set the current culture to German (which uses “,” as a decimal character) it also validates that correctly with no change.
    <asp:TextBox ID="txtDoubleType" CssClass="input" runat=""server"" Width="200″>
    </asp:TextBox>
    
    <asp:CompareValidator ID="cmprValidatorDoubleType " ControlToValidate="
        txtDoubleType" Type="Double" Display="Dynamic" Operator="DataTypeCheck"
        ErrorMessage="*Not a valid number." runat="server"<>/asp:CompareValidator>
  • This validates Currency
    <asp:TextBox ID="txtCurrencyType" CssClass="input" runat=""server"" Width="200″>
    </asp:TextBox>
    
    <asp:CompareValidator ID="cmprValidatorDoubleType " ControlToValidate="
        txtCurrencyType" Type="Currency" Display="Dynamic" Operator="DataTypeCheck"
        ErrorMessage="*Not a valid amount." runat="server"<>/asp:CompareValidator>

Enjoy coding….!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)