html5新增的用戶端校正

來源:互聯網
上載者:User

標籤:birt   back   校正   沒有   ack   input   自訂   表單   郵箱   

1.表單控制項提供的checkValidity()方法進行校正。

例如:如果checkValidity()方法返回true
則表明該表單內的所有表單控制項都有效。

<body>    <form action="">        生日:<input id="birth" name="birth" type="date"><br> 郵件地址:        <input type="email" id="email" name="email"><br>        <input type="submit" value="提交" onclick="return check()"></form><script>    var check = function () {        return commonCheck(‘birth‘, ‘生日‘, ‘欄位必須是有效日期‘)            && commonCheck(‘email‘, ‘郵箱‘, ‘欄位必須符合電子郵件的格式‘)    }    var commonCheck = function (fieid, fieName, tip) {        var targetEle = document.getElementById(fieid);        if (targetEle.value.trim() == ‘‘) {            alert(fieName + ‘欄位必須填寫‘);            return false        }        else if (!targetEle.checkValidity()) {            alert(fieName + tip);            return false        }        return true    }</script></body>

2.自訂驗證  應用h5新增的setCustomValidity()方法實現。只有當該表單沒有通過校正時才能調用該方法。

例如:

<body>    <form action="">       圖書名:<input id="name" name="name" type="text" required><br>         圖書ISBN:<input type="text" id="isbn" name="isbn" required pattern="\d{3}-\d-\d{3}-\d{5}"        ><br>           圖書價格:<input type="number" id="price" name="price" required min="20" max="150" step="5"        ><br>        <input type="submit" value="提交" onclick="check()"></form><script>    var check = function () {      if(!document.getElementById(‘name‘).checkValidity()){document.getElementById(‘name‘).setCustomValidity(‘圖書名是必須的‘);      }            if(!document.getElementById(‘isbn‘).checkValidity()){document.getElementById(‘isbn‘).setCustomValidity(‘圖書isbn是必須的‘+‘\n而且必須符合xxx-x-xxx-xxxxx的格式(其中x代表數字)‘);      }            if(!document.getElementById(‘price‘).checkValidity()){document.getElementById(‘price‘).setCustomValidity(‘價格是必須的‘+‘\n而且必須在20-150之間,且是5的倍數‘);      }    }    </script>

 

html5新增的用戶端校正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.