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

How to prevent the input of more than 13 digits?

    3 answers 3

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

    \D means NOT the number. And additionally we take strictly no more than 13 characters.

    • well suited, only I would like to have a pure regular expression, and I have a question to enter only numbers (but I already knew what to replace with \ d) - Karalahti
    • @Karalahti Instead of substr you can of course use the second replace, but it is the second one! it is impossible to do it by one regular basis. The problem is that as soon as at least one letter is removed, execution will continue from this point, and the counting of characters for any other constraints from 1. - Mike
    • @Karalahti With one regular schedule, you can only check for letters that are not letters or more than 13 characters, but you cannot use such a regular schedule in replace - Mike
    • understood thanks! - Karalahti
     $('.number').live('keydown', function() { var checkingRegExp = new RegExp(/^(\d){1,13}$/g); return $(this).val().match(checkingRegExp) !== null; }); 
    • var checkingRegExp = /^\d{1,13}$/; it is better. - Wiktor Stribiżew 5:52

    You can use a quantifier in which you specify a range

     /^(\d){1,13}$/g 
    • Your function was not written correctly. - user3240895
    • And you checked what you wrote?))) I need to enter only numbers and no more than 13))) - Karalahti
    • @Karalahti So here, please, regex101.com Check my regular expression for correctness. - user3240895 1:51
    • I'm not talking about the syntoxys of the code, but about the question, it was completely different, entering only numbers in the field and no more than 13 characters per line! Mike suggested, and jsfiddle.net/AJIEKCEU/8h4akhwu started working , try typing something into the field! All but the numbers are cut off and do not enter more than 13 characters - Karalahti