表單驗(yàn)證幾乎在每一個(gè)需要注冊(cè)或者登陸的網(wǎng)站是必不可少的,有些驗(yàn)證則非常的復(fù)雜,可以說(shuō)是各種各樣給你的要求,不過(guò)本章節(jié)只介紹一下表單中最簡(jiǎn)單的驗(yàn)證方式,就是判斷是否為空,有些要求比較低的網(wǎng)站對(duì)此已經(jīng)滿足需要了。
代碼如下:
<html> <head> <meta charset="gb2312"> <title>js簡(jiǎn)單表單驗(yàn)證</title> <script type="text/javascript">window.onload=function(){ var bt=document.getElementById("bt"); bt.onclick=function() { if(document.myform.name.value=="") { alert("用戶名不能為空!"); document.myform.name.focus(); return false; } else if(document.myform.pw.value=="") { alert("密碼不能為空!"); document.myform.pw.focus(); return false; } }}</script></head><body><form action="index.php" method="get" name="myform"><ul> <li>姓名:<input type="text" name="name" id="name" /></li> <li>密碼:<input type="text" name="pw" id="age" /></li> <li><input type="submit" id="bt"/></li></ul> </form></body></html> 以上就是本文的詳細(xì)內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。



















