# Thread: 1 Decimal Validation

1. ## 1 Decimal Validation

Dear All,
I can check now a field is isNumeric via this method var isNumeric = /^[0-9.]+\$/;. 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?

2. Originally Posted by newtomysql
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?
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.

3. 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.

4. Originally Posted by newtomysql
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:
/^[0-9]+{2}(\.[0-9])?\$/;

5. Originally Posted by newtomysql
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.
One option:

Code:
```
<script type="text/javascript">

function validateNum(num){
var regex = /^\d{2}(\.\d)?\$/;
if(regex.test(num)) {
} else {
}
}

</script>```

6. Something like the following should do.

/^[0-9]+(\.[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.

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

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

Code javascript:
```function validateNum(value){
var num = Number(value);
if (num >= 10 && num < 100) {
} else {
}
}```

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

8. Originally Posted by paul_wilkins
Another option, which removed the need for a regular expression completely is:

Code javascript:
```function validateNum(value){
var num = Number(value);
if (num >= 10 && num < 100) {
} else {
}
}```

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.

9. Originally Posted by webdev1958
But the 23.12345 alert's 'valid' which is not what the op wants. They specified only 1 decimal place after the decimal.
Ahh yes, that would involve another check for something like:
Math.floor(num * 10) === num * 10
which can serve to complicate things.

Code javascript:
```function validateNum(value){
var num = Number(value);
if (num >= 10 && num < 100 && Math.floor(num * 10) === num * 10) {
} else {
}
}```

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

Code javascript:
```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) {
} else {
}
}```

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

10. Originally Posted by paul_wilkins
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

Code:
```function validateNum(num){
var regex = /^[1-9][0-9](\.\d)?\$/;
if(regex.test(num)) {
} else {
}
}```
Personally I find the regex code easier to read.

11. Dear All,
I have tried both the method var isDecimal = /^[1-9][0-9](\.\d)?\$/; and /^[0-9]+{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.

12. Originally Posted by newtomysql
Dear All,
I have tried both the method var isDecimal = /^[1-9][0-9](\.\d)?\$/; and /^[0-9]+{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.
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

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•