2011-04-25 80 views
1

這個驗證和重定向的問題出現在輸入時間短於5位數字時(它應該是5纔有效)。如果它的5,它確定用戶被重定向,因爲它應該是。正如我所說,問題出現時,它無效(短於5)。如果你能告訴我如何解決這個問題,我會很感激!先謝謝你們。這裏是代碼:Javascript表單驗證和重定向

 <script type="text/javascript"> 
     function redirect(){ 
      var zip_c = document.myform.tx1.value; 
      var sid = document.myform.sid.value; 
      if ((myform.tx1.value.length == 5) || (myform.textfielda.value.length > 20)){ 
       /* if (zip_c == "") 
       { 
        return false; 
       } 
       */ 
       var redirect = "http://www.yourcarinsurancefinders.com/lowest-rates/quotes/ppv_hc01/src/results/index.php?zip="+ zip_c+"&sid="+sid; //Edint redirection link here, but dont change '?zip="+ zip_c' 
       window.open(redirect,'blank'); 
      } 
      if (myform.tx1.value == ""){ 

       document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code."; 
       return false; 
      } 
      if ((myform.tx1.value.length < 5) || (myform.textfielda.value.length > 20) || (myform.tx1.value.length == 0)) 
      { 
       document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code."; 
       return false; 
      } 

      return false; 
     } 

    </script> 

,這裏是我的形式:

<form onSubmit="return redirect();" id="myform" name="myform" method="" action="">      
       <div id="tx_a"><div class="inText">Zip Code:</div><input type="text" class="numeric" name="tx1" id="tx1" maxlength="5" /></div> 
       <div id="button"><input type="submit" name="submit" value="" /></div> 
       <input type="hidden" id="sid" name="sid" value="<?php echo $sid; ?>" /> 
</form> 

我發現我自己的解決方案:

function redirect(){ 
      var zip_c = document.myform.tx1.value; 
      var sid = document.myform.sid.value; 
      if ((myform.tx1.value.length < 5) || (myform.tx1.value.length == 0)) 
      { 
       document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code."; 
       return false; 
      } 
      if ((myform.tx1.value.length == 5) || (myform.textfielda.value.length > 20)){ 
       /* if (zip_c == "") 
       { 
        return false; 
       } 
       */ 
       var redirect = "http://www.yourcarinsurancefinders.com/lowest-rates/quotes/ppv_hc01/src/results/index.php?zip="+ zip_c+"&sid="+sid; //Edint redirection link here, but dont change '?zip="+ zip_c' 
       window.open(redirect,'blank'); 
      }              
     } 

回答

1

如果要重定向,你可以只返回true,並將其提交給表單的操作。 或者您可以使用window.location = 'whatever.com';