當前位置:編程學習大全網 - 源碼下載 - js表單驗證:字數驗證、郵箱格式、字母開頭,重復密碼

js表單驗證:字數驗證、郵箱格式、字母開頭,重復密碼

<html>

<head>

<script type="text/javascript">

function validate()

{

var ok=true,un,pw1,pw2,el,ch0;

var r = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;

if(""==(un=document.f.user.value))

{

alert("用戶名不能為空!");

document.f.user.focus();

ok=false;

}

else if(""==(pw1=document.f.password1.value))

{

alert("密碼不能為空!");

document.f.password1.focus();

ok=false;

}

else if(""==(pw2=document.f.password2.value))

{

alert("重復密碼不能為空!");

document.f.password2.focus();

ok=false;

}

else if(""==(el=document.f.email.value))

{

alert("郵箱不能為空!");

document.f.email.focus();

ok=false;

}

if(ok)

{

if(!(un.length>=6 && un.length<=20))

{

alert("用戶名長度必須大於等於6小於等於20!");

document.f.user.focus();

ok=false;

}

else

{

ch0=un.charAt(0).toLowerCase();

if(!(ch0>="a" && ch0<="z"))

{

alert("用戶名必須以字母開頭!");

document.f.user.focus();

ok=false;

}

else if(pw1!=pw2)

{

alert("重復密碼與密碼必須相同!");

document.f.password2.focus();

ok=false;

}

else if(!r.test(el))

{

alert("不是有效的郵箱地址!");

document.f.email.focus();

ok=false;

}

}

}

if(ok)

{

alert("恭喜妳,通過了有效性驗證!");

}

}

</script>

</head>

<body>

<form name="f">

用戶名:<input type="text" name="user" /><br /><br />

密碼:<input type="text" name="password1" /><br /><br />

重復密碼:<input type="text" name="password2" /><br /><br />

郵箱:<input type="text" name="email" /><br /><br />

<input type="button" value="驗證" onclick="validate();" />

</form>

</body>

</html>

  • 上一篇:如何將PHP session信息緩存到memcached裏面
  • 下一篇:定量趨勢源代碼
  • copyright 2024編程學習大全網