欢迎光临
我们一直在努力

常用javascript验证代码

建站超值云服务器,限时71元/月

//把下面的文字保存为js文件
//******************************************************
//验证是否为空,如果为空返回 false,否则返回 true 
function CheckIsNull(Inform,Inputname){ 
    var Form=Inform+"." 
 eval("Temp="+Form+Inputname+".value;"); 
 if(Temp==""){ 
  alert("提醒您:不能为空"); 
  eval(Form+Inputname+".className='RedInput';"); 
  eval(Form+Inputname+".focus();"); 
  return false; 
 }else{ 
  eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
     return true; 
 } 

//验证是否为数字 
function CheckIsNum(Inform,Inputname){ 
    if (!CheckIsNull(Inform,Inputname))return false; 
 else{ 
          var Form=Inform+"." 
          eval("Temp="+Form+Inputname+".value;"); 
       if(isNaN(Temp)){ 
                     alert("提醒您:不为数字"); 
                     eval(Form+Inputname+".className='RedInput';"); 
                     eval(Form+Inputname+".focus();"); 
                     return false; 
                      } 
    else{ 
         eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
            return true; 
           } 
     } 

//验证是否为E-MAIL 
function CheckIsEmail(Inform,Inputname){ 
    if (!CheckIsNull(Inform,Inputname))return false; 
    else{ 
       var Form=Inform+"." 
          eval("Temp="+Form+Inputname+".value;"); 
       if(Temp.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1) 
           { alert("提醒您:不为EMAIL"); 
             eval(Form+Inputname+".className='RedInput';"); 
             eval(Form+Inputname+".focus();"); 
             return false; 
              } 
    else{ 
          eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
             return true; 
        } 
       }   

//验证是否为HTTP地址 
function CheckIsHttp(Inform,Inputname){ 
    if (!CheckIsNull(Inform,Inputname))return false; 
    else{  
         var Form=Inform+"." 
   eval("Temp="+Form+Inputname+".value;"); 
   if(Temp.search(/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/)==-1) 
         { alert("提醒您:不为HTTP"); 
             eval(Form+Inputname+".className='RedInput';"); 
             eval(Form+Inputname+".focus();"); 
             return false; 
              } 
      else{ 
           eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
              return true; 
         } 
      } 

//验证是否为手机号码 
function CheckIsMobile(Inform,Inputname){ 
    if (!CheckIsNull(Inform,Inputname))return false; 
    else{ 
   var Form=Inform+"." 
   eval("Temp="+Form+Inputname+".value;"); 
   if(Temp.search(/^1[3|5]\d{9}$/)==-1) 
   {   alert("提醒您:不为手机号码"); 
    eval(Form+Inputname+".className='RedInput';"); 
    eval(Form+Inputname+".focus();"); 
    return false; 
   } 
   else{ 
            eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
               return true; 
        } 
         } 

//*****************************
使用方法
index.html
<script language="javascript" src="***.js"></script>
//总验证函数 
//调用参数说明:(formname,inputname) 其中formname为表单名,inputname为输入文本框名 
function CheckForm(){ 
 if(!CheckIsHttp("myform","mytxt")) return false; 

//–> 
</script> 
<style type="text/css"> 
.RedInput {background-color:#FF0000;} 
</style> 
<form id="myform" name="myform" method="post" action=""onSubmit="return CheckForm()"> 
  <input type="text" name="mytxt" /> 
  <input type="submit" name="Submit" value="提交" /> 
</form> 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 常用javascript验证代码
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址