Click here to Skip to main content
15,861,168 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
16 Sep 2010CPOL 33.2K   2   4
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….!

This article was originally posted at http://blog.taknikkar.com?p=13

License

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


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

Comments and Discussions

 
GeneralMy vote of 3 Pin
Gaurang Naik17-Jun-12 23:00
Gaurang Naik17-Jun-12 23:00 
QuestionGood one Pin
Gaurang Naik17-Jun-12 22:59
Gaurang Naik17-Jun-12 22:59 
GeneralMy vote of 4 Pin
jesuspinedaq@gmail.com19-Jul-11 11:43
jesuspinedaq@gmail.com19-Jul-11 11:43 
GeneralMy vote of 5 Pin
j03x218-Sep-10 4:35
j03x218-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    Praise Praise    Rant Rant    Admin Admin   

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