最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does validity.valid works? - Stack Overflow

programmeradmin1浏览0评论

I was trying to set an input number (html) to only positive numbers and I found this fine solution:

    <input type="number" name="test_name" min="0" oninput="validity.valid|| 
    (value='');">

Can anyone tell me how does oninput="validity.valid||(value=''); works? How does it restrict the input to only positive numbers.

Thank you!

I was trying to set an input number (html) to only positive numbers and I found this fine solution:

    <input type="number" name="test_name" min="0" oninput="validity.valid|| 
    (value='');">

Can anyone tell me how does oninput="validity.valid||(value=''); works? How does it restrict the input to only positive numbers.

Thank you!

Share Improve this question asked Dec 9, 2018 at 23:40 Juan PabloJuan Pablo 1851 gold badge1 silver badge10 bronze badges 1
  • 1 Full guide here: developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/… – Blue Commented Dec 9, 2018 at 23:49
Add a comment  | 

2 Answers 2

Reset to default 10

min="0" only accept numbers greater than zero. So when the user enters a value (oninput), either it is valid (validity.valid) or (||) the value is replaced by an empty string (value='').


Edit:

validity.valid is falsy because of min="0" as we can see in the doc under the rangeUnderflow property:

"if the value is less than the minimum specified by the min attribute".

This event is sent when a user enters text in a input.

This event is onlycalled when the text displayed would change, thus it is not called when the user presses non-displayable keys.

So what is does is to check the validity.valid property to make the validation.

发布评论

评论列表(0)

  1. 暂无评论