0

I have checked the below link for regular expression

Regex to match 2 digits, optional decimal, two digits

regular expression should accept whole number or decimal points. max length should be 10 numbers before decimal and 4 digits after decimal.

tried this below code from above link (2 digit decimal point)

\d{0,2}(\.\d{1,2})?

var patt =new RegExp("\d{0,2}(\.\d{1,2})?")
undefined
patt.test(1)
true
patt.test(1.1)
true
patt.test(11)
true
patt.test(111.111)
true

for 3 digits after decimal also it is giving true value, which is invalid.

Community
  • 1
  • 1
SivaRajini
  • 7,225
  • 21
  • 81
  • 128

4 Answers4

9

You have to use delimiters, to determine where your match starts and where it ends. And you say max length should be 10 numbers before decimal and 4 digits after decimal so your limits are incorrect too:

var patt = /^\d{1,10}(\.\d{1,4})?$/;

var patt = new RegExp(/^\d{1,10}(\.\d{1,4})?$/);

console.log( patt.test(1) );                // true
console.log( patt.test(1.1) );              // true
console.log( patt.test(11.11) );            // true
console.log( patt.test(111.111) );          // true
console.log( patt.test(1111.1111) );        // true
console.log( patt.test(1111111111.1111) );  // true

console.log( patt.test(111.11111) );        // false, because to long after decimal
console.log( patt.test(11111111111.1111) ); // false, because to long before decimal

Or as suggested here, not using RegExp and instead use a literal. There could be support issues with RegExp in some browsers. The output is the same, so better use this solution:

var patt = /^\d{1,10}(\.\d{1,4})?$/;

console.log( patt.test(1) );                // true
console.log( patt.test(1.1) );              // true
console.log( patt.test(11.11) );            // true
console.log( patt.test(111.111) );          // true
console.log( patt.test(1111.1111) );        // true
console.log( patt.test(1111111111.1111) );  // true

console.log( patt.test(111.11111) );        // false, because to long after decimal
console.log( patt.test(11111111111.1111) ); // false, because to long before decimal
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • You are making that same mistake - use a regex literal. Use `/^\d{1,10}(\.\d{1,4})?$/` – Wiktor Stribiżew Aug 25 '16 at 06:55
  • `RegExp("/^\d{1,10}(\.\d{1,4})?$/")` won't work at all. – Wiktor Stribiżew Aug 25 '16 at 06:55
  • @eisbehr thanks for your response. please provide the working fiddle. it is not working as expected. – SivaRajini Aug 25 '16 at 06:58
  • Added example @SivaRajini – eisbehr Aug 25 '16 at 07:01
  • @WiktorStribiżew Why would `RegExp` not work? Please tell me the problem of using it. It workes great, see my example. – eisbehr Aug 25 '16 at 07:02
  • 3
    It works because you removed the double quotes and turned a C string into a regex literal. Mind that `RegExp` constructor with a regex literal as first argument does not work in all browsers. – Wiktor Stribiżew Aug 25 '16 at 07:05
  • Thanks for your explaination. Didn't know that there are browsers not supporting it. Thought it is basic. @WiktorStribiżew – eisbehr Aug 25 '16 at 07:06
  • \ in a string escapes the next character of the string. The RegExp object never receives the backslash. \\ would be required in a string but that makes it (even more) complicated to read, so use the regex object literal to avoid it. – jedifans Aug 25 '16 at 07:07
2

The regex doesn't say the string should end with the last character, or start with the first one. Try:

var patt = /^\d{1,2}(\.\d{1,2})?$/;

For your 10 digits before and 4 digits after requirement:

var patt = /^\d{1,10}(\.\d{1,4})?$/;
jedifans
  • 2,287
  • 1
  • 13
  • 9
0

Why don't you just check:

 input < Math.pow(10,10) && input % Math.pow(10,-4) == 0
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
-1

here is the code that is working for me

 function IsValidNumericCode(number,messageDiv,msg){
 var reNumeric = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
 var result  = reNumeric.test(number); if(!result){ messageDiv.html(msg);
 messageDiv.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
 }else{ messageDiv.html('');} return result;
MaxMini
  • 39
  • 1
  • 11