2012-07-07 73 views
-1

這裏是我的驗證代碼:使表單驗證返回true?

function validateEnquiryForm() { 
     var x = document.forms["enquiry"]["name"].value; 
     if(x == null || x == "" || x == "Name") { 
      $('#name').removeClass('custom').addClass('error'); 
      $('.name-error').show(); 
      //return false; 
     } else if(x !== null || x !== "" || x !== "Name") { 
      $('#name').removeClass('error').addClass('custom'); 
      $('.name-error').hide(); 
     } 
     var x = document.forms["enquiry"]["phone"].value; 
     if(x == null || x == "" || x == "Phone Number") { 
      $('#phone').removeClass('custom').addClass('error'); 
      $('.phone-error').show(); 
      //return false; 
     } else if(x !== null || x !== "" || x !== "Phone Number") { 
      $('#phone').removeClass('error').addClass('custom'); 
      $('.phone-error').hide(); 
     } 
     var x = document.forms["enquiry"]["requirements"].value; 
     if(x == null || x == "" || x == "Add any other extra requirements here...") { 
      $('#requirements').removeClass('textarea-1').addClass('error'); 
      $('.requirements-error').show(); 
      //return false; 
     } else if(x !== null || x !== "" || x !== "Add any other extra requirements here...") { 
      $('#requirements').removeClass('error').addClass('custom'); 
      $('.requirements-error').hide(); 
     } 
     var x = document.forms["enquiry"]["summary"].value; 
     if(x == null || x == "" || x == "Summarise your project in a few key words...") { 
      $('#summary').removeClass('custom').addClass('error'); 
      $('.summary-error').show(); 
      //return false; 
     } else if(x !== null || x !== "" || x !== "Summarise your project in a few key words...") { 
      $('#summary').removeClass('error').addClass('custom'); 
      $('.summary-error').hide(); 
     } 
     var x = document.forms["enquiry"]["email"].value; 
     var atpos = x.indexOf("@"); 
     var dotpos = x.lastIndexOf("."); 
     if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) { 
      $('#email').removeClass('custom').addClass('error'); 
      $('.email-error').show(); 
      //return false; 
     } else if(atpos > 1 || dotpos > atpos + 2 || dotpos + 2 <= x.length) { 
      $('#email').removeClass('error').addClass('custom'); 
      $('.email-error').hide(); 
     } 
     $('.error-banner').effect("pulsate", { 
      times: 2 
     }, 1000); 
     return false; 
    } 

當代碼運行時,它會檢查所有的領域,看看他們是空白。如果.error-banner脈衝中有一個或多個填充不正確並顯示單個消息。問題是當表單填寫正確時,此橫幅仍然閃爍。有人能告訴我我要去哪裏嗎??

+1

那麼函數**總是**返回'false'。你如何期待它做別的事情? – Pointy 2012-07-07 14:41:49

+1

此外,jQuery與'document.forms'的奇怪組合是......奇怪的。 – Pointy 2012-07-07 14:42:15

+1

聽說過數據結構和代碼可讀性嗎? – Engineer 2012-07-07 14:42:34

回答

2

據我所知,你的功能與此塊結束時,無條件地:

$('.error-banner').effect("pulsate", { 
    times: 2 
}, 1000); 
return false; 

它也許改爲:

var valid = true; 

// in your error handling conditionals, set valid to false: 
if(x == null || x == "" || x == "Phone Number") { 
    $('#phone').removeClass('custom').addClass('error'); 
    $('.phone-error').show(); 
    valid = false; 
} 

... 

if (!valid) { 
    $('.error-banner').effect("pulsate", { 
    times: 2 
    }, 1000); 
} 
return valid; 

我與大多數評論者的同意,您的儘管如此,仍有很大的改進空間。

+0

我是非常新的Javascript和jQuery,我只是使用我知道現在得到的 – user1449737 2012-07-07 14:46:28

+0

也你的代碼工程就像一個魅力!非常感謝你。 (將在4分鐘內接受答案) – user1449737 2012-07-07 14:50:02

相關問題