1 Decimal Validation

Dear All,
I can check now a field is isNumeric via this method var isNumeric = /[1]+$/;. My problem now user can put two or more decimal and I want to limit just to single decimal and only 2 numbers before the decimal? How to edit please?


  1. 0-9. ↩︎

What does this mean? Decimal numbers or decimal points?

Does it mean 2 or more decimal points or decimal numbers. Are decimal points mandatory or optional. Do you want up to 2 numbers before the decimal point or up to 2 numbers before the decimal point?

Post a pattern of the format you want and describe which components are mandatory and which are optional otherwise people will have to guess or make assumptions.

Something like the following should do.

/[1]+(\.[0-9]+)?$/;

[0-9]+ is for any number of numerals before the decimal point
(…)? indicates that the contents are to be considered optional
\. within the optional section is for a fullstop
[0-9]+ after the fullstop is for any number of numbers following it

As an extra, of you don’t want a back-reference created for the parenthesis part, you can use ?: at the start of the parenthesis content to tell the regex engine not to do so. See the backreferences section for more details on this.

/[2]+(?:\.[0-9]+)?$/;


  1. 0-9 ↩︎

  2. 0-9 ↩︎

Dear Webdev,
So samples which I want to allow will be 23, 23.1,56.1,67 etc. Meaning that I want only 2 number before the decimal and one number after the decimal. If it is not decimal number then just limit two digits too. Thank you. Hope I am clearer. The minimum will 2 digit numbers and that is mandatory.

Will it always be two before the decimal point? If so, that would be:
[0-9]+{2}
but if it’s 1 or 2 allowed before the decimal point, such as with 9.5, then it would be:
[0-9]+{1,2}

An updated regular expression to account for this new information, with a mandatory 2 digits before and an optional decimal value of one digit, is:
/[1]+{2}(\.[0-9])?$/;


  1. 0-9 ↩︎

One option:

        
      <script type="text/javascript">

            function validateNum(num){
                [COLOR=#ff0000]var regex = /^\\d{2}(\\.\\d)?$/;[/COLOR]
                if(regex.test(num)) {
                    alert('valid');
                } else {
                    alert('invalid');
                }
            }

        </script>

Another option, which removed the need for a regular expression completely is:


function validateNum(value){
    var num = Number(value);
    if (num >= 10 && num < 100) {
        alert('valid');
    } else {
        alert('invalid');
    }
}

Number(value) converts it to a number, which can include decimal values too.

But 23.12345 alert’s ‘valid’ which is not what the op wants. They specified only 1 decimal place after the decimal point.

Ahh yes, that would involve another check for something like:
Math.floor(num * 10) === num * 10
which can serve to complicate things.


function validateNum(value){
    var num = Number(value);
    if (num >= 10 && num < 100 && Math.floor(num * 10) === num * 10) {
        alert('valid');
    } else {
        alert('invalid');
    }
}

So how can things be made less complicated. By off-loading some of the work to other functions?


function isTwoDigitNumber(num) {
    return num >= 10 && num < 100;
}
function decimalPlaces(num) {
    return num.toString().split('.')[1].length;
}
function validateNum(value){
    var num = Number(value);
    if (TwoDigitNumber(num) && decimalPlaces(num) <= 1) {
        alert('valid');
    } else {
        alert('invalid');
    }
}

The code is a lot easier to read and understand that way, but a regular expression might be preferred.

I guess whether it is easier to read or not boils down to personal preference. For me, the less code I have to read the easier it is.

The only thing the op didn’t specify is if something like 00.1 or 08 is allowed. If it isn’t then it’s only a minor tweak in the regex

function validateNum(num){
                [COLOR=#ff0000]var regex = /^[1-9][0-9](\\.\\d)?$/;[/COLOR]
                if(regex.test(num)) {
                    alert('valid');
                } else {
                    alert('invalid');
                }
            }

Personally I find the regex code easier to read.

Dear All,
I have tried both the method var isDecimal = /[1]0-9?$/; and /[2]+{2}(\.[0-9])?$/;. I get an error invalid quantifier at ^. I am hopefully clear again I would accept 2.3, 23.3,45.2,12.4. I can accept one or two number before decimal but after decimal only 1 single number.


  1. 1-9 ↩︎

  2. 0-9 ↩︎

I’m not sure how you expect anyone to tell you what you have done wrong if you don’t post your code. Look at the demo code I posted. It works without problems in my IE9 and FF8.

Also, earlier you said

The minimum will 2 digit numbers and that is mandatory
and now you say

I can accept one or two number before decimal…

It appears to me either you don’t know what you are doing or you are making this up as you go along. If you now accept 1 or 2 digits before the decimal then it’s only a very minor change to the code I posted. You should be able to figure it out for yourself. I don’t do spoon feeding.

I won’t be spending anymore time on this just in case you make more things up :slight_smile:


  1. 1-9 ↩︎

  2. 0-9 ↩︎

Dear Webdev,
Ok I manage to solve my problem and here is the final solution as benefit for the rest too. /[1]{1,2}(\.[0-9])?$/ This regex works fine thank you for the time and courage and also to Paul for his ideas too.


  1. 0-9 ↩︎