调用该方法,可以显示对表单元素进行有效验证,返回值是boolean。

代码如下:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<title></title> 
</head> 
<body> 
<form onsubmit="check()" novalidate="true"> 
<label for="email"> 邮箱:</label> 
<input type="email" id="email" name="email"/> 
<input type="submit" value="提交"> 
</form> 
</body> 


<script> 
function check(){ 
var email=document.getElementById('email'); 
if(email.value==''){ 
alert('请输入email!'); 
return false; 
}else if(!email.checkValidity()){ 
alert('email的格式不正确!'); 
return false; 
}
else
alert("您输入的email地址有效") 

</script> 
</html>

注:novalidate="true"用来取消表单的默认验证方式

 posted on 2017-07-10 20:21  Lu7  阅读(1718)  评论(0编辑  收藏  举报