JavaScript | Validation of numbers on a form in jQuery

When working with a variety of forms, there is often a need for data validation in jQuery, before submitting the form to PHP.

A simple solution is to use the script below, which has the task of checking the text typed in the controls type of number". The script works fine in every browser and allows the use of the key "backspace" and "delete".

$(document).on('keydown', 'input[type="number"]', function(event) {    if ( event.keyCode == 46 || event.keyCode == 8 ) {    }    else {       if (event.keyCode < 48 || event.keyCode > 57 ) {       event.preventDefault();    } else {       if($(this).is('[data-length]')) {          if($(this).val().length >= $(this).attr('data-length')) {             event.preventDefault(); return false;          }          } else if($(this).val().length >= 3) {             event.preventDefault(); return false;         }       }    } });

Contact