Jquery/Javascript Regular expression to filter decimal numbers with comma, 13 int places and 2 decimal places

Ale Cataalyst

Hi I need help with an input. I need to make a regular expression that filter it on keypress.

The input will have a number with max 13 int places and max 2 decimal places. In addition the decimal number uses a comma instead of dot.

I tried some regex but they don't works on keypress.

111111,11 TRUE
1111111111111 TRUE 
11111111111111 FALSE
11111111111111,11 FALSE
1111111111111,11 TRUE
111,111 FALSE
1a FALSE
1.0 FALSE
1.00 FALSE
0 TRUE

I tried the following regular expression, and tested it on regextester website. In that site it partially works (some cases still not working), but on javascript it doesn't filter. I don't know the reason.

/^(\d{1,13})(\,)?(\d{1,2})$/g

This is the jquery method

$(".numericWithDecimal").on("keypress keyup blur",function (event) {
    var regex = /^(\d{1,13})(\,)?(\d{1,2})$/g;

   //I tried this
   if(!regex.match($(this).val()))
     event.preventDefault();

   //or this
   if (!regex.test($(this).val()))
     event.preventDefault();

});

Is possible to create an expression that works with these cases? What is?

[EDIT] New method based on @The fourth bird regex

 $(".numericWithDecimalLimit").on("keypress keyup blur",function (event) {
         var regex = /^(?:\d{1,13})(?:,\d{1,2})?$/g;
         var value = this.value + String.fromCharCode(event.keyCode || event.charCode);     
         if(!regex.test(value))
             event.preventDefault();    
     });
The fourth bird

You could update your regex to and include the comma into a non capturing group and make it optional:

^(\d{1,13})(?:,\d{1,2})?$

That would match:

  • ^ Beginning of the string
  • (\d{1,13}) Capture 1 - 13 digits in a group
  • (?: Non capturing group
    • , Match a comma
    • \d{1,2} Match 1 or 2 digits
  • )? Close non capturing group and make it optional
  • $ End of the string

For the match you could also make the first group a non capturing group:

^(?:\d{1,13})(?:,\d{1,2})?$

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular Expression that matches number with max 2 decimal places

From Dev

Display decimal 2 places if float, and display decimal 0 places if int

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Fixing values/numbers to 2decimal places

From Dev

Ruby output numbers to 2 decimal places

From Dev

2 decimal places inputmask

From Dev

Excel Numbers with two decimal places

From Dev

Regular expression that matches only up to 10 decimal places

From Dev

Format a number with comma separators and round to 2 decimal places in Python 2?

From Dev

Regex for formatting comma for decimal places and dot for thousands

From Dev

Format decimal value to currency with 2 decimal places

From Java

Round a double to 2 decimal places

From Dev

Preventing rounding to 2 decimal places

From Dev

Rounding to 2 decimal places in SQL

From Dev

Get NSDecimalNumber with 2 decimal places

From Dev

Highchart axis 2 decimal places

From Dev

How to print in 2 decimal places?

From Dev

Converting Double to 2 Decimal Places

From Dev

Converting data into 2 decimal places

From Dev

Get NSDecimalNumber with 2 decimal places

From Dev

Formatting with 2 decimal places (NSDecimalNumber)

From Dev

Format number to 2 decimal places only if it has decimal places

From Dev

Round to at most 2 decimal places (only if it has decimal places) (JavaScript)

From Dev

converting all the numbers in a javascript array to 2 decimal places within a function

From Dev

RegExpValidator take in numbers with . with two decimal places

From Dev

Select numbers with more than 4 decimal places

From Dev

Force numbers in list to two decimal places

From Dev

Regex to match non-negative numbers, no leading zeros, 2 decimal places, at least 1 digit, optional decimal

Related Related

  1. 1

    Regular Expression that matches number with max 2 decimal places

  2. 2

    Display decimal 2 places if float, and display decimal 0 places if int

  3. 3

    Convert a double to 2 decimal places with comma separator

  4. 4

    Convert a double to 2 decimal places with comma separator

  5. 5

    Fixing values/numbers to 2decimal places

  6. 6

    Ruby output numbers to 2 decimal places

  7. 7

    2 decimal places inputmask

  8. 8

    Excel Numbers with two decimal places

  9. 9

    Regular expression that matches only up to 10 decimal places

  10. 10

    Format a number with comma separators and round to 2 decimal places in Python 2?

  11. 11

    Regex for formatting comma for decimal places and dot for thousands

  12. 12

    Format decimal value to currency with 2 decimal places

  13. 13

    Round a double to 2 decimal places

  14. 14

    Preventing rounding to 2 decimal places

  15. 15

    Rounding to 2 decimal places in SQL

  16. 16

    Get NSDecimalNumber with 2 decimal places

  17. 17

    Highchart axis 2 decimal places

  18. 18

    How to print in 2 decimal places?

  19. 19

    Converting Double to 2 Decimal Places

  20. 20

    Converting data into 2 decimal places

  21. 21

    Get NSDecimalNumber with 2 decimal places

  22. 22

    Formatting with 2 decimal places (NSDecimalNumber)

  23. 23

    Format number to 2 decimal places only if it has decimal places

  24. 24

    Round to at most 2 decimal places (only if it has decimal places) (JavaScript)

  25. 25

    converting all the numbers in a javascript array to 2 decimal places within a function

  26. 26

    RegExpValidator take in numbers with . with two decimal places

  27. 27

    Select numbers with more than 4 decimal places

  28. 28

    Force numbers in list to two decimal places

  29. 29

    Regex to match non-negative numbers, no leading zeros, 2 decimal places, at least 1 digit, optional decimal

HotTag

Archive