当前位置:   article > 正文

表单自定义验证setCustomValidity

setcustomvalidity

对于默认的表单验证,有时候不是很友好,所以我们可以使用自定义表单验证,如下所示:


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <form action="#">
  9. <input type="text" class="name" placeholder="username" οninput="validateUername(this)">
  10. <input type="submit">
  11. </form>
  12. <script type="text/javascript">
  13. function validateUername(input){
  14. if(input.value.length < 3){
  15. input.setCustomValidity("用户名长度不得小于3位");
  16. }else{
  17. input.setCustomValidity("");
  18. }
  19. }
  20. </script>
  21. </body>
  22. </html>

在我们点击提交的时候,如果不符合要求,会出现下图所示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/110824
推荐阅读
  

闽ICP备14008679号