admin管理员组文章数量:1418861
Suppose you have an input for email like this:
<input type='email' placeholder='Email:' required />
In Chrome and other modern browsers having type set to email enables validation i.e. only email like "something@something" will be valid. It displays default popup message like this:
I was wondering if it is possible to tap into these events in order to use them for a custom validation? And disable default browser ones.
Suppose you have an input for email like this:
<input type='email' placeholder='Email:' required />
In Chrome and other modern browsers having type set to email enables validation i.e. only email like "something@something" will be valid. It displays default popup message like this:
I was wondering if it is possible to tap into these events in order to use them for a custom validation? And disable default browser ones.
Share Improve this question asked Jun 2, 2016 at 14:18 IljaIlja 46.6k103 gold badges289 silver badges528 bronze badges2 Answers
Reset to default 4First you need to disable default browser validation. To do so, just add novalidate
to form element:
<form class="custom-validated-form" novalidate >
<input type='email' placeholder='Email:' required />
</form>
To do some custom validation you need to catch the submit event and do .preventDefault()
in case your custom validation failed:
$('.custom-validated-form').submit(function(event) {
// your custom validation
event.preventDefault()
})
Good idea might be to use third-party library such as parsley.js to handle the validation for you.
You can disable client-side HTML5 browser validation by adding the novalidate
flag to your form
tag:
<!-- This validates as normal -->
<form method="post" action="#">
<input type="email" placeholder="Validate Email:" required />
<input type="submit">
</form>
<!-- This adds the `novalidate` flag -->
<form method="post" action="#" novalidate>
<input type="email" placeholder="Novalidate Email:" required />
<input type="submit">
</form>
This will allow you to still use HTML input types (such as "email") and prevent the browser from running validations.
From that point, you would be open to implementing your own solution, or integrating one of many third-party validation plugins (such as jQuery Validation).
Furthermore, if you would like to keep the HTML5 validation, but alter the validation message, you can use setCustomValidity -- It may present some browser difficulties, but it's a viable option none the less.
本文标签: javascriptOverwrite default browser form validationStack Overflow
版权声明:本文标题:javascript - Overwrite default browser form validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745299850a2652304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论