By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Check textbox value...

P: n/a
NH
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...
Jan 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"NH" <NH@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
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...

Jan 5 '06 #2

P: n/a
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

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"NH" wrote:
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...

Jan 5 '06 #3

P: n/a
NH
Thanks for your suggestions, Philips looks like the one to use.

Regards
N

"Phillip Williams" wrote:
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

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"NH" wrote:
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...

Jan 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.