Javascript - validation, numbers only

前端 未结 14 1695
感动是毒
感动是毒 2020-12-08 09:52

I\'m trying to get my login form to only validate if only numbers were inputted. I can it to work if the input is only digits, but when i type any characters after a number,

14条回答
  •  抹茶落季
    2020-12-08 10:40

    // I use this jquery it works perfect, just add class nosonly to any textbox that should be numbers only:

    $(document).ready(function () {
           $(".nosonly").keydown(function (event) {
               // Allow only backspace and delete
               if (event.keyCode == 46 || event.keyCode == 8) {
                   // let it happen, don't do anything
               }
               else {
                   // Ensure that it is a number and stop the keypress
                   if (event.keyCode < 48 || event.keyCode > 57) {
                  alert("Only Numbers Allowed"),event.preventDefault();
                   }
               }
           });
       });
    

提交回复
热议问题