Check textbox value...

G

Guest

How can I check the value of a textbox has no more than 2 decimal places?

I already check that the value is numeric, but how do I check that it is not
0.223 or .022 etc. I only want to allow up to 2 decimal places...
 
M

Marina

Turn it into a string, and look at the position of the decimal point in the
string.
If it's more then the 3rd character from the end, then there are too many
decimal points.
 
G

Guest

A regular expression to check for a number with 2 decimals looks like this
"^(\+|-)?\d{1,6}(\.\d{1,2})?$". You may use it in a
RegularExpressionValidator:

<asp:TextBox ID="txtNumber" Runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="valTxtNumber"
ControlToValidate="txtNumber" Runat="Server" ValidationExpression
="^(\+|-)?\d{1,6}(\.\d{1,2})?$" ErrorMessage="Number is not valid. Please
enter numbers with 2 decimals only" > </asp:RegularExpressionValidator>

Or you may use it in a Javascript like this:
var re = /^(\+|-)?\d{1,6}(\.\d{1,2})?$/g;
alert(re.test("123456.123")); //this will echo false
alert(re.test("56.23")); //this will echo true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top