Javascript regex to match only up to 11 digits, one comma, and 2 digits after it
问题 I have a textbox where I want only allowed up to 11 digits, an optional comma, and two more digits after it. Anything else should not be rendered when the key is pressed into the textbox: $('#txt').keypress(function (e) { var code = e.which; var key = String.fromCharCode(code); // REGEX TO AVOID CHARS & A DOT (.) var pattern = /[a-zA-Z]|\./g; var isMatch = pattern.test(key); if (isMatch) { // DO NOT RENDER CHARS & dot e.preventDefault(); } }); The above code works when an invalid key is