用户单击“提交”按钮时,校验文本框中的录入,并按照js代码中的文本进行校验提示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM</title>
</head>
<body>
<form>
<input id="count" type="number" step="2" min="0" max="20">
<input type="submit" value="提交">
</form>
<script type="text/javascript" language="javascript">
var quantity = document.getElementById("count");
quantity.onblur = function(){
if(quantity.validity. stepMismatch ){
quantity.setCustomValidity("值无效,需要偶数值");
}else if(quantity.validity. rangeUnderflow ){
quantity.setCustomValidity("最小值为2");
}else if(quantity.validity. rangeOverflow ){
quantity.setCustomValidity("最大值为20");
}else if(quantity.validity.customError){
quantity.setCustomValidity("");
}
}
</script>
</body>
</html>