Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 16 Sep 2010
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

Syed Mujtaba Hassan
Software Developer
Pakistan Pakistan
Syed Mujtaba Hassan
Software Engineer
Intelligentsia Software (Pvt) Ltd Islamabad Pakistan.

Comments and Discussions

 
GeneralMy vote of 3 PinmemberGaurang Naik17-Jun-12 23:00 
QuestionGood one PinmemberGaurang Naik17-Jun-12 22:59 
GeneralMy vote of 4 Pinmemberjesuspinedaq@gmail.com19-Jul-11 11:43 
GeneralMy vote of 5 Pinmemberj03x218-Sep-10 4:35 
Nice. Definitely something I'm going to use Wink | ;-)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 16 Sep 2010
Article Copyright 2010 by Syed Mujtaba Hassan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid