Home javascript Regular expression, input only numbers and no more than 13

Regular expression, input only numbers and no more than 13

Author

Date

Category

when typing in a field, you need to prohibit entering everything except numbers, and entering no more than 13 characters. Digits only ready input.value.replace (/ [^ \ d] / g, ''); .

How to prevent input of more than 13 digits?


Answer 1, authority 100%

input.value.replace (/ \ D / g, ''). substr (0,13)

\ D – means “NOT a digit”. And in addition, we take strictly no more than 13 characters.


Answer 2, authority 50%

$ ('. number'). live ('keydown', function () {
  var checkingRegExp = new RegExp (/ ^ (\ d) {1,13} $ / g);
  return $ (this) .val (). match (checkingRegExp)! == null;
});

Answer 3

You can use a range quantifier

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions