標籤: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新增的用戶端校正