Submit HTML5 Form using Javascript and validate its inputs

后端 未结 3 1662
我在风中等你
我在风中等你 2020-12-18 20:01

I\'m writing form and adding html5 validation attributes to its input like \"required\", \"autofocus\". I use Javascript to submit the form using document.myForm.submit() bu

3条回答
  •  眼角桃花
    2020-12-18 20:17

    The pimvdb's answer is based on a workaround to include a hidden submit button.

    HTML5 provides javascript functions to achieve the same thing without a submit button. As Tigraine pointed out, Mozilla documents two validity checking methods for 'form' elements:

    • checkValidity() return true/false and doesn't show anything to the end user
    • reportValidity() return true/false and ALSO shows the validation messages to the end user (like the normal submit button click does)

提交回复
热议问题