Javascript - validation, numbers only

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

    Match against /^\d+$/. $ means "end of line", so any non-digit characters after the initial run of digits will cause the match to fail.

    Edit:

    RobG wisely suggests the more succinct /\D/.test(z). This operation tests the inverse of what you want. It returns true if the input has any non-numeric characters.

    Simply omit the negating ! and use if(/\D/.test(z)).

提交回复
热议问题