?? validate.js
字號:
//驗證用戶名.
function validateusername()
{
var uname=document.frm1.username.value;
var str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var ch1,ch2;
ch1=uname.charAt(0); //得到uname的首字符.
ch2=uname.charAt(uname.length-1); //得到uname的尾字符.
//判斷username文本框的格式。(不能為空、不能小于3位、必須以字母或數字開頭或結尾<indexOf()方法的結果為-1表示找不到要找的字符>)
if(uname=="" || uname.length<3 || str.indexOf(ch1,0)==-1 || str.indexOf(ch2,0)==-1 || !isSsnString(uname))
{
document.getElementById("Layer1").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer1").style.visibility="hidden";
return true;
}
}
//驗證密碼.
function validatepassword()
{
var pwd=document.frm1.password.value;
if(pwd=="" || pwd.length<6)
{
document.getElementById("Layer2").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer2").style.visibility="hidden";
return true;
}
}
//驗證密碼確認.
function validateconfirmpassword()
{
var pwd=document.frm1.password.value
var cpwd=document.frm1.confirmPassword.value;
if(cpwd=="" || cpwd!=pwd)
{
document.getElementById("Layer3").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer3").style.visibility="hidden";
return true;
}
}
//驗證密碼提示問題.
function validatequestion()
{
var question=document.frm1.question.value
if(question=="")
{
document.getElementById("Layer4").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer4").style.visibility="hidden";
return true;
}
}
//驗證密碼提示答案.
function validateanswer()
{
var answer=document.frm1.answer.value;
if(answer=="" || answer.length<6)
{
document.getElementById("Layer5").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer5").style.visibility="hidden";
return true;
}
}
//驗證出生年份.
function validateYear()
{
var year=document.frm1.year.value;
if(year.length!=4 || isNumberString(year,"1234567890")!=1 || year<1902 || year>2006)
{
document.getElementById("Layer6").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer6").style.visibility="hidden";
return true;
}
}
//驗證出生日期.
function validateDate()
{
var year=document.frm1.year.value;
var month=document.frm1.month.value;
var day=document.frm1.day.value;
if (((Math.abs(Math.abs(month*2-15)-5)==2&&(day==31))||(month==2&&((day>28&&parseInt(year)%4!=0)
||(day>29&&parseInt(year)%4==0)))))
{
document.getElementById("Layer6").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer6").style.visibility="hidden";
return true;
}
}
//驗證證件號碼.
function validateidCard()
{
var idType=document.frm1.idType.value;
var idCard=document.frm1.idCard.value;
if(idType==0)
{
if(idCard.length!=15 && idCard.length!=18)
{
document.getElementById("Layer7").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer7").style.visibility="hidden";
return true;
}
}
else
{
if(idCard.length<6)
{
document.getElementById("Layer8").style.visibility="visible";
return false;
}
else
{
document.getElementById("Layer8").style.visibility="hidden";
return true;
}
}
}
//驗證電子郵箱.
function validateEmail()
{
var email=document.frm1.email.value;
var length=email.length;
var AtSym=email.indexOf("@");
var period=email.indexOf(".");
var space=email.indexOf(" ");
var ch=email.charAt(length-1)
var str=".@~!#$%^&*()_+|\=-}{';:?/.,<>";
if(length>0)
{
if(AtSym<1 || period<=AtSym+1 || period==length-1 || space!=-1 || str.indexOf(ch)!=-1)
{
document.getElementById("Layer9").style.visibility="visible";
return false;
}
}
else
{
document.getElementById("Layer9").style.visibility="hidden";
return true;
}
}
function DIVhidden1()
{
document.getElementById("Layer1").style.visibility="hidden";
}
function DIVhidden2()
{
document.getElementById("Layer2").style.visibility="hidden";
}
function DIVhidden3()
{
document.getElementById("Layer3").style.visibility="hidden";
}
function DIVhidden4()
{
document.getElementById("Layer4").style.visibility="hidden";
}
function DIVhidden5()
{
document.getElementById("Layer5").style.visibility="hidden";
}
function DIVhidden6()
{
document.getElementById("Layer6").style.visibility="hidden";
}
function DIVhidden78()
{
document.getElementById("Layer7").style.visibility="hidden";
document.getElementById("Layer8").style.visibility="hidden";
}
function DIVhidden9()
{
document.getElementById("Layer9").style.visibility="hidden";
}
//驗證表單是否能提交.
function validateform()
{
if(validateusername()&&validatepassword()&&validateconfirmpassword()&&validatequestion()&&validateanswer()&&validateYear()&&validateDate()&&validateidCard()&&validateEmail())
{
return true;
}
else
{
return false;
}
}
//驗證用戶名是否為指定字符組成.
function isSsnString (ssn)
{
var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
if(re.test(ssn))
return true;
else
return false;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -