验证API
两个方法:
checkValidity():如果input元素中的数据是合法的返回true,否则返回false;
setCustomValidity():设置input元素的validationMessage属性,用于自定义错误提示信息的方法
使用setCustomValidity设置自定义后提示后,validity.customError就会变成true,checkValidity总会返回false。如需重新判断需要取消自定义提示:
setCustomValidity('')
setCustomValidity(null)
setCustomValidity(undefined)
checkValidity()方法
例:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>javascript 验证API</title> 6 </head> 7 <body> 8 <p>输入数字并验证</p> 9 <input type="number" id="id1" min="100" max="300" required> 10 <button onclick="myFunction()">验证</button> 11 <p id="demo"></p> 12 </body> 13 <script> 14 function myFunction(){ 15 // 获取用户输入对象 16 var inpobj = document.getElementById("id1"); 17 console.log(inpobj) 18 // checkValidity():如果用户输入是合法的,返回true,否则返回false 19 if(inpobj.checkValidity() == false){ 20 document.getElementById("demo").innerHTML=inpobj.validationMessage; 21 } 22 else{ 23 document.getElementById("demo").innerHTML="nice got it..." 24 } 25 } 26 </script> 27 </html>
标签:inpobj,setCustomValidity,false,验证,JavaScript,checkValidity,API From: https://www.cnblogs.com/wenzm/p/17056313.html