• Javascript验证


    // JavaScript Document


    /**
    调用方法:
    1.addcheck(id,str);
    2.addcheck(id,str,type);
    3.addcheck(id,str,type,value);
    4.addcheck(id,str,type,value,errorid);

    id为检查的控件的id,比如 input text 类型的id .
    str为出错误提示的字符串
    type为检查的类型.具体数据请查看checkOne(one)里的字符串 ,如 "null" ,检查是否为空。不能为空。
    value为附加值,比如说检查长度,我们则可以附加个长度。
    errorid为出错误要显示的id的层次。假如不设置该属性,请增加setError(str)方法来处理错误。

    另外还有addcheckTemp方法,可以增加属性,不过该方法增加的属性只检查一次。
    如:
    addcheckTemp(id,str);
    */

    //addcheck("ename","姓名不能为空!","","","t_ename");
    //addcheck("ename","姓名必须为汉字!","cword","","t_ename");
    //addcheck("ename","用户名长度不能少于3位!","length","3","t_ename");
    //addcheck("pass","密码不能为空!","","","t_pass");
    //addcheck("twopass","第二次不能为空!");
    //addcheck("pass","密码长度不能少于6位!","length","6","t_pass");
    //addcheck("pass","第二次密码必须等于第一次密码!","equls","twopass","t_twopass");

    var CheckObj=[];
    var tempCheckObj=[];


    function addcheck()
    {
    var one=new checkModel();
    one.Set(arguments);
    CheckObj.push(one);
    }

    function addcheckTemp()
    {
    var one=new checkModel();
    one.Set(arguments);
    tempCheckObj.push(one);
    }

    function setErrorByCheck(str,id)
    {
    if(id==null||id=="")
    {
       if(str!=""){
        setError(str);
       }
    }
    else
    {
       document.all[id].innerText=str;
    }
    }

    function checkModel(){
    this.id="";
    this.str="";
    this.type="null";
    this.value="0";
    this.errorid="";

    this.Set=function(arg){
       var a=arg;
       var pos=0;
       for(i=0;i<a.length;i=i+1){
        if (a[i].indexOf("#")!=0){
         if(pos==0){
          pos=pos+1;
          if(a[i]==null||a[i]=="")
          {
           continue;
          }
          this.id=a[i];
          continue;
         }else if(pos==1){
          pos=pos+1;
          if(a[i]==null||a[i]=="")
          {
           continue;
          }
          this.str=a[i];
          continue;
         }else if(pos==2){
          pos=pos+1;
          if(a[i]==null||a[i]=="")
          {
           continue;
          }
          this.type=a[i];
          continue;
         }else if(pos==3){
          pos=pos+1;
          if(a[i]==null||a[i]=="")
          {
           continue;
          }
          this.value=a[i];
          continue;
         }else if(pos==4){
          pos=pos+1;
          if(a[i]==null||a[i]=="")
          {
           continue;
          }
          this.errorid=a[i];
          continue;
         }
        }
       }
    };
    }


    function checkForm()
    {
    for(i=0;i<CheckObj.length;i++)
    {
       if(checkOne(CheckObj[i])==false)
       {
        document.all[CheckObj[i].id].focus();
        return false;
        break;
       }
       else
       {
        setErrorByCheck("",CheckObj[i].errorid);
       }
    }

    for(i=0;i<tempCheckObj.length;i++)
    {
       var tempobj=tempCheckObj.pop();
       if(checkOne(tempobj)==false)
       {
        document.all[tempobj.id].focus();
        return false;
        break;
       }
       else
       {
        setErrorByCheck("",Ctempobj.errorid);
       
       }
    }
    return true;
    }

    function checkOne(one)
    {
    var check=new checkMethod();
    if(one.type=="null")   //验证是否为空
    {
       if( !check.checkNull(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="length")   //验证长度是否达到
    {
       if( !check.checkLength(one.id,one.value) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="email")   //验证Email格式是否正确
    {
       if( !check.checkEmail(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="equls")   //验证是否相等
    {
       if( !check.checkEquls(one.id,one.value) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="phone")   //验证电话格式是否正确
    {
       if( !check.checkPhone(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="mobile")   //验证手机格式是否正确
    {
       if( !check.checkMobile(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="num")    //验证是否为数字
    {
       if( !check.checkNumber(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="eword")   //验证是否为英文单词
    {
       if( !check.checkEWord(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="sign")    //验证是否为符号
    {
       if( !check.checkSign(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="cword")   //验证是否为中文
    {
       if( !check.checkCWord(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else if(one.type=="date")   //验证是否为中文
    {
       if( !check.checkDate(one.id) )  
       {
        setError(one.str,one.errorid);
        return false;
       }
    }else     //假如验证类型为其他,则直接返回False
    {
       return false;
    }
    return true;
    }

    function checkMethod()
    {
    this.checkNull=function(id)
    {
       if(document.all[id].value=="")
       {
        return false;
       }
       return true;
    }

    this.checkLength=function(id,len){    //验证长度
       if(document.all[id].value.length>=len)
       {
        return true;
       }else
       {
        return false;
       }
    };

    this.checkEmail=function(id)    //验证是否为E-mail
    {
       var emailPat=/^(.+)@(.+).(.+)$/;//邮箱格式正则表达式
       var matchArray;
       var email=document.all[id].value;
       if(email=="")
       {
        return true;
       }
       matckArray=email.match(emailPat);     
       if(matckArray==null||matchArray==""){
        return false;
       }
       else{
        return true;
       }
    };

    this.checkEquls=function(id1,id2){   //验证是否相等
       if(document.all[id1].value==document.all[id2].value)
       {
        return true;
       }else
       {
        return false;
       }
    };

    this.checkPhone=function(id){    //验证固定电话
       var phoneCode=document.all[id].value;
       if(phoneCode=="")
       {
        return true;
       }
       var filter0=/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
       var filter1=/^(([0\+]\d{2,3}-)?(0\d{2,3}))?(\d{7,8})(-(\d{3,}))?$/;
       return filter0.test(phoneCode)||filter1.test(phoneCode);   
       /*
       var strSource="0123456789-";
       for(i=0;i<phoneCode.length;i=i+1){     
        if(strSource.indexOf(phoneCode.substring(i,i+1))==-1){      
         return false;
        }          
       }
       //判断 '-' 的出现次数,假如出现大于一次,则验证错误
       var n=(s.split('-')).length-1;
       if(n>1){
        return false;
       }
       return true;
       */   
    };

    this.checkMobile=function(id){    //验证手机
       var partten = /^1[3,5]\d{9}$/;   
       var movePhone=document.all[id].value;
       if(movePhone=="")
       {
        return true;
       }
       if(!partten.test(movePhone)){
        return false;
       }
       else{
        return true;
       }
    };

    this.checkNumber=function(id){   //验证数字
       var str=document.all[id].value;
       if(str=="")
       {
        return true;
       }
       if(/[\D]/g.test(str)){
        return false;
       }
       return true;
    };

    this.checkEWord=function(id){   //验证英文
       var str=document.all[id].value;
       if(str=="")
       {
        return true;
       }
       if(/[^a-z]/gi.test(str)){
        return false;
       }
       return true;
    };

    this.checkSign=function(id){   //验证字符
       var str=document.all[id].value;
       if(str=="")
       {
        return true;
       }
       if(/[\u4E00-\u9FA5a-z0-9\s]/gi.test(str)){
        return false;
       }
       return true;
    }

    this.checkCWord=function(id)    //验证是否为中文
    {
       var reg=/^[u4E00-u9FA5]+$/;//中文正则表达式
       var i;    
       var tureName=document.all[id].value;
       if(tureName=="")
       {
        return true;
       }
       for(i=0;i<tureName.length;i=i+1){//验证每一个字符是否为中文
        var sbTureName=tureName.substring(i,i+1);
        if(reg.test(sbTureName)){
         return false;
        }    
       }
       return true;
    };

    this.checkDate=function(id)
    {
       INDate=document.all[id].value
       if(INDate=="")
       {
        return true;
       }

       subYY=INDate.substr(0,4)
       if(isNaN(subYY) || subYY<=0){
           return false;
       }
       //转换月份
       if(INDate.indexOf('-',0)!=-1)
       {
        separate="-"
       }
       else
       {
           if(INDate.indexOf('/',0)!=-1)
        {
         separate="/"
        }
           else {
         return false;
        }
       }
          area=INDate.indexOf(separate,0)
          subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))
          if(isNaN(subMM) || subMM<=0)
       {
           return false;
       }
          if(subMM.length<2)
       {
        subMM="0"+subMM;
       }
       //转换日
       area=INDate.lastIndexOf(separate)
       subDD=INDate.substr(area+1,INDate.length-area-1)
       if(isNaN(subDD) || subDD<=0){
           return false;
       }
       if(eval(subDD)<10){
        subDD="0"+eval(subDD)
       }
       NewDate=subYY+"-"+subMM+"-"+subDD
       if(NewDate.length!=10){return false;}
         if(NewDate.substr(4,1)!="-"){return false;}
         if(NewDate.substr(7,1)!="-"){return false;}
       var MM=NewDate.substr(5,2);
       var DD=NewDate.substr(8,2);
       if((subYY%4==0 && subYY%100!=0)||subYY%400==0)
       { //判断是否为闰年
           if(parseInt(MM)==2)
        {
            if(DD>29){return false;}
       }
       }else{
           if(parseInt(MM)==2){
            if(DD>28){return false;}
           }
       }
       var mm=new Array(1,3,5,7,8,10,12); //判断每月中的最大天数
       for(i=0;i< mm.length;i=i+1){
           if (parseInt(MM) == mm[i]){
            if(parseInt(DD)>31){return false;}
           }else{
            if(parseInt(DD)>30){return false;}
           }
       }
       if(parseInt(MM)>12){return false;}
          return true;
    };
    }

  • 相关阅读:
    实验一、词法分析实验
    词法分析程序新
    词法分析程序
    我对编译原理的理解
    Unity3d数据存储 PlayerPrefs,XML,Json数据的存储与解析
    Unity3d网络总结(三) 封装Socket创建简单网络
    Unity3d网络总结(二)使用NetWorking代码搭建简单的网络模块
    Unity3d网络总结(一) NetWork组件使用
    Unity加载AB包
    unity编辑器拓展
  • 原文地址:https://www.cnblogs.com/KingStar/p/1712811.html
Copyright © 2020-2023  润新知