需要javascript表单验证最终结果

时间:2011-05-20 06:28:05

标签: javascript

您为我的联系页面写了一个javascript验证。为此,我使用了开关盒功能。这很好。但我无法将最终结果视为真或假。这意味着如果所有表单都有效,那么结果应为true,否则为false。我如何从我的功能中获得结果。

这是我写的函数(我喜欢使用这个核心javascript):

window.onload = function (){
    var subButton = document.getElementById("submit");
    subButton.onclick = function (){
        var myForm = document.getElementsByTagName('form')[0];
        var myFormStatus = false;
        myElements = myForm.elements;
            for(i=0;i<myElements.length;i++){
                ele = myElements[i];
                if(ele.type == 'text' || ele.type == 'textarea'){
                    switch(ele.id){
                        case 'name':
                        case 'company':
                        case 'message':
                        var valueCheck = textValidation(ele.value);
                        if(valueCheck){
                            var errorSpan = spanMaker(ele.nextSibling);
                            myFormStatus = false;
                        }
                        else{
                                var hideSpan = spanHider (ele.nextSibling);
                                myFormStatus = true;
                        }
                        break;
                        case 'email': 
                            var emailCheck = emailValidation(ele.value);
                            if(!emailCheck){
                                var errorSpan = spanMaker (ele.nextSibling);
                                myFormStatus = false;
                            }
                            else{
                                var hideSpan = spanHider (ele.nextSibling);
                                myFormStatus = true;
                            }
                        break;
                        case 'phone': 
                        var phoneNum = regIsNumber(ele.value);
                        if(!phoneNum){
                            var errorSpan =spanMaker (ele.nextSibling);
                            myFormStatus = false;
                        }
                        else{
                                var hideSpan = spanHider (ele.nextSibling);
                                myFormStatus = true;
                            }
                        break;
                        default :
                        break;
                    }



                }

                function textValidation (text){
                    return !/[^\s]/.test(text); 
                }

                function emailValidation (email){
                    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                    return String(email).search (reg) != -1;
                }

                function regIsNumber(fData){
                     var isInteger_re     = /^[\d|\+|\(]+[\)|\d|\s|-]*[\d]$/;
                     return String(fData).search (isInteger_re) != -1;
                }

                function spanMaker(element){
                    while(element && element.nodeType !=1){
                        element = element.nextSibling;
                        element.style.display = 'block';

                    }   
                }

                function spanHider (element){
                    while(element && element.nodeType !=1){
                        element = element.nextSibling;
                        element.style.display = 'none';

                    }   
                }

            }
        }
    }   

任何好的建议?

2 个答案:

答案 0 :(得分:0)

也许您可以将myFormStatus初始化为true,然后使用逻辑运算符'&amp;'在每个布尔值上为每个验证案例(1个假,你的最后一个结果为假,所有为真,使你的最后结果为真)

编程时,一个好的真值表总是有用的; - )

答案 1 :(得分:0)

更改为

window.onload = function (){

  var myForm = document.getElementsByTagName('form')[0];
  var myFormStatus = true;
  myForm.onsubmit = function (){

    for(i=0;i<myElements.length;i++){
      ele = myElements[i];
      if(ele.type == 'text' || ele.type == 'textarea'){
         var hideSpan = spanHider (ele.nextSibling);

         switch(ele.id){

           case 'name':
           case 'company':
           case 'message':
             var valueCheck = textValidation(ele.value);
             if(valueCheck){ // should really return FALSE if invalid
               var errorSpan = spanMaker(ele.nextSibling);
               myFormStatus = false;
             }
             break;
           case 'email': 
             var emailCheck = emailValidation(ele.value);
             if(!emailCheck){
               var errorSpan = spanMaker (ele.nextSibling);
               myFormStatus = false;
             }
             break;
           case 'phone': 
             var phoneNum = regIsNumber(ele.value);
             if(!phoneNum){
               var errorSpan =spanMaker (ele.nextSibling);
               myFormStatus = false;
             }
             break;
          } // switch
        }  // if
      }  // for
    } // onsubmit
    return myFormStatus
  }