1. 程式人生 > >JavaScript-運用正則表示式檢驗表單

JavaScript-運用正則表示式檢驗表單

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8"/> 
          <style>
    span{
        display:none;
        color: red;
    }
    </style>       
    </head>
    <body>
        <input type="text"
id="box"/>
<span>請填寫數字</span> </br> <input type="password" id="psw"/><span>還沒有填哦</span> <br/> <input type="submit" value="提交"/> <a href="javascript:;">點我 </a> </body> <script> var
oId=document.getElementById("box"); var Span=document.getElementsByTagName("span"); var myregex=/\d{8}/ oId.onfocus=function(){ if(myregex.test(oId.value)) Span[0].style.display="none"; else Span[0].style.display="inline"; } oId.onblur=function(){ if
(myregex.test(oId.value)) Span[0].style.display="none"; else Span[0].style.display="inline"; } document.getElementsByTagName('a')[0].onclick=function(){alert(oId.value);}
</script> </html>