Javascript - validation, numbers only

前端 未结 14 1711
感动是毒
感动是毒 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

    The simplest solution.

    Thanks to my partner that gave me this answer.

    You can set an onkeypress event on the input textbox like this:

    onkeypress="validate(event)"

    and then use regular expressions like this:

    function validate(evt){
         evt.value = evt.value.replace(/[^0-9]/g,"");
    }
    

    It will scan and remove any letter or sign different from number in the field.

提交回复
热议问题