js正規表現は携帯電話番号、ユーザー名とメールアドレスを検証します.
5845 ワード
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hangge.com</title>
</head>
<script> // function $sel(id,tabname){ if(id!="" && tabname!=""){ var tem_obj=document.getElementById(id); return tem_obj.getElementsByTagName(tabname); }else if(id!=""){ return document.getElementById(id); }else{ return document.getElementsByTagName(tabname); } } // function isnum(obj){ var reg=/^1[0-9]{10}/; if(!reg.test(obj.value)){ alert(" !"); obj.value=""; } } // function ismail(obj){ var reg=/[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,5}\.[a-zA-Z0-9]{1,5}/; if(!reg.test(obj.value)){ alert(" !"); obj.value=""; } } // function isname(obj){ var reg=/^[\u4e00-\u9fa5]{2,4}$/; if(!reg.test(obj.value)){ alert(" ! 。"); obj.value=""; } } // function init(){ // $sel("phone","").onblur=function(){ isnum(this); } $sel("mail","").onblur=function(){ ismail(this); } $sel("username","").onblur=function(){ isname(this); } } </script>
<body onload="init()">
<input type="text" id="username" placeholder=" "/><br><br>
<input type="text" id="phone" placeholder=" "/><br><br>
<input type="text" id="mail" placeholder=" "/>
</body>
</html>