TKCSCS 2022-01-17 17:45 采纳率: 100%
浏览 39
已结题

javascript前端校验,使用setCustomValidity()自定义提示信息时未达到预期效果


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>前端校验:用户注册</title>
    <style>
        input:invalid {
            background-color:red;
        }
        input:focus:invalid{
            background-color:white;
        }
    </style>
</head>
<body>
    <form id="registerForm">
        <table>
            <tr>
                <td><label for="username">请设置用户名</label></td>
                <td><input type="text" id="username" name="username" required pattern="^[^\s'&quot]{1,20}$"></td>
                <td>1-20个字符,不可以包含空格、引号</td>
            </tr>
            <tr>
                <td><label for="password">请设置密码</label></td>
                <td><input type="password" id="password" name="password" required pattern="^[0-9a-zA-Z]{6,12}$"></td>
                <td>6-12个数字、大小写字母的组合</td>
            </tr>
            <tr>
                <td><label for="birthday">出生日期</label></td>
                <td><input type="date" id="birthday" name="birthday" min="1900-01-01" required></td>
                <td></td>
            </tr>
        </table>
        <input type="submit" value="提交">
    </form>
</body>
    <script>
        var password=document.getElementById("password");
        var birthday=document.getElementById("birthday");
        var form=document.getElementById("registerForm");
        //1、用ajax做用户名校验(略)
        //2、自定义密码校验的提示信息(前端校验方法一)
        password.addEventListener("input",checkPassword);
        password.addEventListener("invalid",customMessage);
        function checkPassword(){
            password.setCustomValidity("");
            password.checkValidity();
        }
        function customMessage(){
            if(password.validity.valueMissing){
                password.setCustomValidity("您必须设置一个密码");
            }
            else if(password.validity.patternMismatch){
                password.setCustomValidity("请设置正确的密码格式:6-12个数字、大小写字母的组合");
            }else{
                password.setCustomValidity("");
            }
        }
        //3、自定义出生日期的提示信息(前端校验方法二)
       form.addEventListener("submit",checkBirthday1);
       birthday.addEventListener("change",checkBirthday2);
       function checkBirthday1(event){
           if(birthday.checkValidity()){
               birthday.setCustomValidity("");
           }else{
               showErro();
               event.preventDefault();
           }
       }
       function checkBirthday2(){
           if(birthday.checkValidity()){
               birthday.setCustomValidity("");
           }else{
               showErro();
           }
       }
       function showErro(){
           if(birthday.validity.valueMissing){
               birthday.setCustomValidity("请输入您的生日");
           }else if(birthday.validity.rangeOverflow){
               birthday.setCustomValidity("您必须年满18周岁才能注册");
           }else if(birthday.validity.rangeUnderflow){
               birthday.setCustomValidity("日期非法");
           }else{
               birthday.setCustomValidity("请输入一个有效值");
           }
           birthday.reportValidity();
       }
        window.onload=function(){
            setMaxBirthday();
        }
        function setMaxBirthday(){
            var nowDate=new Date();
            var year=nowDate.getFullYear()-18;
            var month=nowDate.getMonth()+1;
            var date=nowDate.getDate();
            birthday.max=year+"-"+"0"+month+"-"+date;
        }
    </script>
</html>

我预期的效果:如果没有输入生日,前端校验报错“请输入您的生日”
实际表现的效果:如果没有输入生日,前端校验报错“请填写此字段”

为何setCustomValidity()的效果没有实现?

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-01-17 20:45
    关注

    oninvalid+onchange/或者oninput清除错误

    img

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>前端校验:用户注册</title>
    </head>
    <body>
        <form id="registerForm">
            <table>
                <tr>
                    <td><label for="birthday">出生日期</label></td>
                    <td><input type="date" id="birthday" name="birthday" min="1900-01-01" required oninvalid="this.setCustomValidity('请输入您的生日')" onchange="this.setCustomValidity('');"></td>
                    <td></td>
                </tr>
            </table>
            <input type="submit" value="提交">
        </form>
    </body>
    <script>
        window.onload = function () {
            setMaxBirthday();
        }
        function setMaxBirthday() {
            var nowDate = new Date();
            var year = nowDate.getFullYear() - 18;
            var month = nowDate.getMonth() + 1;
            var date = nowDate.getDate();
            birthday.max = year + "-" + "0" + month + "-" + date;
        }
    </script>
    </html>
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月9日
  • 已采纳回答 8月1日
  • 创建了问题 1月17日

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装