Maison > interface Web > tutoriel HTML > 求助!表单验证_html/css_WEB-ITnose

求助!表单验证_html/css_WEB-ITnose

WBOY
Libérer: 2016-06-21 09:38:39
original
930 Les gens l'ont consulté

本帖最后由 zmtwyd 于 2012-10-15 13:52:54 编辑

表单验证的时候   密码和重复密码的时候  如果这样 先输入密码  再输入重复密码(当然2此密码一致) 然后离开焦点 就可以显示2个都是正确的。这事正常的。
但是,
如果先输入重复密码,再输入密码,然后离开焦点的时候 即使2次不一致,也显示的2个都是正确。这是因为输入密码后离开焦点的时候只是验证了自己而没有匹配和下面重复密码的值 ,这个问题怎么解决?
我的思路是 既然重复密码离开焦点的时候验证匹配上面密码的值,那么也让先输入重复密码,再输入密码,然后离开焦点的时候,眼验证下面的重复密码的值。 但如何做到呢???????下面是代码

  
  function checktag(p){                                  
                     var x=p.nextSibling;
                                         while(x.nodeName!="SPAN")
                                         {
                                         x=x.nextSibling
                                         }
                                         return x;
                     }  //获取INPUT后面的SPAN一个方法              

 

//此函数用于传值,有三个参数,第三个是一个函数的指针,它不被执行函数体,所以为FUN
  function yanzhenghanshu(i,ppp,fun,tishi,k) {   
                                       i.onfocus= function gaoliang()  //获取焦点时,触发的方法
                                                                      {
                                                                       if(i.value=="") {checktag(i).innerHTML=ppp; checktag(i).className="d"} 
                                                                       else if(fun()){checktag(i).innerHTML=ppp; checktag(i).className="d"}                      
                                                                       else {checktag(i).innerHTML="请重新输入";checktag(i).className="a"}
                                                                      }



                               
                                      i.onblur= function  likai()     //失去焦点时触发的方法
                                                                      {
                                                                       if(fun()){checktag(i).innerHTML="输入正确";checktag(i).className="c"}
                                                                       else if (i.value==""){checktag(i).innerHTML=ppp;checktag(i).className="a"}
                                                                       else {checktag(i).innerHTML=tishi;checktag(i).className="b"}
                                                                       }
                                      
                                 

                                     
                                      if(k=="hi")                      {
                                                                       if(fun()){checktag(i).innerHTML="输入正确";checktag(i).className="c"}
                                                                       else if (i.value==""){checktag(i).innerHTML=ppp;checktag(i).className="b"}
                                                                       else {checktag(i).innerHTML=tishi;checktag(i).className="b"}
                                                                       }





}



onload=yanzhengaaa;


function yanzhengaaa(k){
var stutas=true;
var qu=document.getElementsByTagName("input")[0];
var bo=document.getElementsByTagName("input")[1];
var o=document.getElementsByTagName("input")[2]; 
var b=document.getElementsByTagName("input")[3]; //获取INPUT对象
 
//以下是:通过一个函数 为每个INPUT的获取和失去焦点时候的状态 传三个值,第三个传的是函数,这个函数叫回调函数
  yanzhenghanshu(qu,"请输入用户名称",function()
                                                {  
                                                  var zhengze=/^[a-z]\w/; var ha=/\s/g;
                                                if(zhengze.test(qu.value)==true && qu.value.length>=6 && qu.value.length                                                                      {return true}
                                                                else
                                                                     { stutas=false; return false}
                                                                   

                                                }
                 ,"输入错误",k);
  yanzhenghanshu(bo,"请输入密码" ,function()
                                                { 
                                                  var zheng=/\w/; var ha=/\s/g;
                                               if(zheng.test(bo.value)==true&&bo.value.length>=6&&bo.value.length                                                                      {return true}
                                                                else
                                                                     {stutas=false;return false}
                                                                      
                                                 }
                 ,"输入错误",k);
  yanzhenghanshu(o,"请再次输入密码",function()   
                                               {  
                                                var zheng=/\w/; var ha=/\s/g;
                                                if(zheng.test(o.value)==true&&o.value.length>=6&&o.value.length                                                                      {return true}
                                                              else 
                                                                     { stutas=false;return false}
                                                                   
                                                }
                ,"输入错误或两次密码不一致",k);

  yanzhenghanshu(b,"请输入邮箱地址",function() 
                                                { 
                                                  var zhengze=/^[a-z]\w/; var ha=/\s/g;
                                                 if(zhengze.test(b.value)==true&&b.value.length>=6&&qu.value.length                                                                      {return true}
                                                              else
                                                                     {stutas=false;return false}
                                                                     
                                                }
                ,"输入错误",k);
           
                                          return stutas;

  }



回复讨论(解决方案)

你可以参考一下这个,很有用而且可以扩展的一个JS文件

http://download.csdn.net/detail/sgyyz/4358592

你应该在表单的onsubmit()事件中验证


<script> <br /> function validchk(){ <br /> ,,,,,,,,,,,,,,,, <br /> } <br /> </script>

就一个验证的,怎么那么多函数,楼上说的对

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal