Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys


I want to validate a text box value, I should allow users to enter values like
1,
1.5,
2,
2.5

it should not take other then 0 or 5 after decimal,

it might be like 10000.5 it should allow.


Advance thanks

Vishwa
Posted

Use the method System.Decimal.TryParse; if it returns true, the string is valid as a decimal representation:
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx[^].

After that, take parsed value and additionally check up for required range or any other constraint.

[EDIT]

OK, for client side, this is a JavaScript variant. Checking up if the numeric value is valid is more tricky:

JavaScript
function isValidFloat(stringValue) {
    value = parseFloat(stringValue); // will return NaN if invalid
    return (value == value); // check value for NaN
}


Note: there is not "decimal", only floats and integers.

When this is done, check for your constraints the same way.

—SA
 
Share this answer
 
v3
Comments
VishwaKL 5-Dec-12 22:49pm    
I want to validate on client side, your answer will help to validate on server side,
Sergey Alexandrovich Kryukov 6-Dec-12 0:10am    
Well, right... can you find analogous approach in JavaScript, or you will need help? This is just a matter of reading documentation...
--SA
VishwaKL 6-Dec-12 0:14am    
Ya i need help for this, i tried with java script mine giving validation but it accept

1.1,1.2---- till 1.5 for all numbers so
Sergey Alexandrovich Kryukov 6-Dec-12 0:27am    
OK, please see the updated answer, after [EDIT].
You could easily find it and experiment by yourself.
--SA
Use This Code:

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server"
        ControlToValidate="TextBox1" ErrorMessage="CompareValidator" Type="Double">Enter Decimal Value</asp:CompareValidator>
        <asp:Button ID="button1" Text="validate" runat="server"/>
    </form>
</body>
</html>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900