日期:2014-05-16  浏览次数:20394 次

form表单javascript验证(防遗忘)
/*去出空格*/
function trim(str){
    for(var  i  =  0  ;  i<str.length  &&  str.charAt(i)=="  "  ;  i++  )  ;
for(var  j  =str.length;  j>0  &&  str.charAt(j-1)=="  "  ;  j--)  ;
if(i>j)  return  "";  
return  str.substring(i,j);  
}
/*检查长度*/
function limitLen(s, Min, Max) {    
    var s = s.trim();    
    if (s == "")
        return false;
    if ((s.length < Min) || (s.length > Max)){
        return false;
    }else{
        return true;
    }
    
}
/* 是否是E-mail */
function isEmail(s) {
    var s = s.trim();
    var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
    return p.test(s);
}
/* 含有中文字符 */
function hasChineseChar(s) {
    var p = /[^\x00-\xff]/;
    return p.test(s);
}
/* 检查邮箱 */
function checkEmail(s) {
    if (s.length < 5) {
        alert("邮件地址输入长度有误!");
        document.regForm.email.focus();
        return false;
    }
    if (!isEmail(s)) {
        alert("邮件地址输入错误!");
        document.regForm.email.focus();
        return false;
    }
    return true;
}
/* 检查密码1 */
function checkPwd(s) {
    if (!limitLen(s, 5, 20)) {
        alert("密码输入长度有误!");
        document.regForm.password.focus();
        return false;
    }
    if (hasChineseChar(s)) {
        alert("密码输入错误!");
        document.regForm.password.focus();
        return false;
    }
    if (limitLen(document.getElementById("repwd").value, 5, 16)) {
        if (trim(document.getElementById("repwd").value) == trim(s)) {
            ;
        } else {
            alert("两次密码输入不一致!");
            document.regForm.password.focus();
            return false;
        }
    }
    return true;
}
/* 检查密码2 */
function checkPwd2(s) {
    if (!limitLen(s, 5, 20)) {
        alert("确认密码入长输度有误!");
        document.regForm.repwd.focus();
        return false;
    }
    if (hasChineseChar(s)) {
        alert("确认密码输入错误!");
        document.regForm.repwd.focus();
        return false;
    }
    if (limitLen(document.getElementById("repwd").value, 5, 16)) {