0

How to do decimal validation using javascript?

There is a text box which should accept only 24.00 or 24 or any value less than 24.00. The text box also must allow if 23.99 is entered.

I tried it this way:

if (document.forms[0].hours!= undefined) {
    var val = document.forms[0].hours.value;
    if (val .match(/^\d{0,6}(?:\.\d{0,2})?$/)) {
        alert("Invalid" +'${payType}'+"  Hours. The hours entered can not have more than 2 decimal places or should be in the range of 0 to 24 ");
        submitted=false;           
        return false;
    }
}

The values can be: 1.00 or 12.25 or 1.25 or 23.99 or 24 but not above these values. Any number below 24.00 or 24.

3
  • use parseFloat() to compare floats? Commented Jul 29, 2014 at 7:12
  • 1
    why dont you use (val < 24); Commented Jul 29, 2014 at 7:13
  • The text box can have values like 23.99 and 24 but not above 24 Commented Jul 29, 2014 at 7:14

4 Answers 4

2

If you prefer doing it with regex try this

^(0+)?(24(\.00?)?|(\.00?)?|\.[0-9]+|0?[0-9](\.[0-9]{0,2})?|(0+)?[0-2][0-3](\.[0-9][0-9]?)?)$

Tested on all the values below

   0.0
  4.00
  4.01
024.0
  5.8
  2.95
 10.5
 10.00
023.9
011
 09.89
 09
  8.67
 24
 24.00
 24.0
 23.99
  0.00
  0
   .5
   .55
  6
 24.01  // fail
 13.90
 78.23  // fail
  1.56
  0.06
 25.00  // fail
 23.99
 41.00  // fail

Demo


The section below isn't related to the answer but rather to the comments below. @Anto these are just a few of online resources on regex. Have fun ;)


Free online books (google for these books)

  • Mastering Regular Expressions, 3rdEdition
  • Introducing Regular Expressions
  • Regular Expression Pocket Reference, 2nd Edition
  • Regular Expressions Cookbook

Good websites

Online regex tester

Sign up to request clarification or add additional context in comments.

13 Comments

it does not allow this value 1.56, we can enter any value below 24
it accepts this value too 24.01 but anything below 24 should be okay
@hex494D49 00.00 too pass, but must be 0.00 or just 0 I think. @Anto its allows 1.56.
@Anto As you can see there's only one fail. I'm trying to fix it :)
@hex494D49, thanks for your response. please help me to fix and let me know once you complete :) thanks
|
2

You could try the below regex,

^(?:24\.00|24|(?:[0][1-9]|[1][0-9]|2[0-3])\.[0-9][0-9])$

DEMO

OR

If you don't want 0 before the digit then you could try the below

^(?:24\.00|24|(?:[1-9]|[1][0-9]|2[0-3])\.[0-9][0-9])$

DEMO

1 Comment

it should also accept 1 to 24 or 1.00 to 23.99 but your regex is not accepting any value between 1 to 24 but 1.00 to 24.00
0

you can reverse thinking

allow any number above 24.00 or 24

var reg = /^(24\.(0[1-9]|[1-9]\d?)|([3-9]\d|[^0]\d{2,9}|2[5-9])(\.\d{1,2})?)$/

     3.33     fail
    23.00     ...
    24        ...
    24.00     ...
     0.00     ...
     0        ...
    24.01     pass
    34.01     ...
    34        ...
   120.18     ...

if (document.forms[0].hours!= undefined) {
    var val = document.forms[0].hours.value;
    if (!reg.test(val)) {
        alert("Invalid" +'${payType}'+"  Hours. The hours entered can not have more than 2 decimal places or should be in the range of 0 to 24 ");
        submitted=false;           
        return false;
    }
}

Comments

0

You don't need regular expression for this at all. In fact it would be a lot simpler to just do this:

if (document.forms[0].hours!= undefined) {
    var val = parseFloat(document.forms[0].hours.value);

    // Get the number of decimals
    var a = val.toString().split(".");
    var decimals = a.length > 1 ? a[1].length : 0;

    if (isNaN(val) || val < 0 || val > 24 || decimals > 2) {
        alert("Invalid" +'${payType}'+"  Hours. The hours entered can not have more than 2 decimal places or should be in the range of 0 to 24 ");
        submitted=false;           
        return false;
    }
}

3 Comments

Fair enough, added isNaN to the if statement to protect from strings as well.
Haha ok, sure. Added parseFloat to protect against empty strings. My point is: regexp for this is to overcomplicate things and will only result in messy code, especially if it's a complicated expression. A logical approach in code is something all (most) developers can read and understand. If you have more requirements, you can simply add it to the if statement.
It is true, but i did not even think the way you thought of. thanks for that. and if i enter space and execute i get Unable to get value of the property 'length': object is null or undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.