2013-05-07 105 views
0

我對javascript稍微陌生,我開始理解這種編碼機制是如何工作的。我創建了一個包含多個字段的簡單html表單。我正在使用JavaScript從現場獲取數據並通過多種驗證功能對其進行驗證。下面的代碼是JavaScript我目前使用:Javascript表單驗證返回

function Validation() 
{ 
    submit_name = document.getElementById('txt_Name').value; 
    submit_surname = document.getElementById('txt_Surname').value; 
    submit_mobilenumber = document.getElementById('MobileNumber').value; 


    if(checkName(submit_name)) 
     { 
    if(checkSurname(submit_surname)) 
      { 
    if(checkMobile(submit_mobilenumber)) 
       { 
       } 
      } 
     } 
    return false; 
} 

我的問題是:在這種代碼的情況下,主要功能(驗證())會遍歷所有的單個功能也一一?

如果例如檢查名()函數返回假,將其它兩個驗證函數checkSurname()和checkMobile()運行或將程序停止在第一個?

的原因,我的問題是,畢竟驗證功能都通過返回我要添加其他功能,以節省一切文件。但是,這隻能在所有表單都通過驗證後才能完成。提前感謝任何幫助。

+2

如果檢查名()失敗的用戶,那麼它不會進入if塊本身,我希望我回答你的問題。 – dreamweiver 2013-05-07 09:47:13

+0

優秀,所以如果checkname失敗,checksurname和checkmobile將無法正確運行? – progdoc 2013-05-07 09:48:25

+1

我建議你對字段進行個別驗證,而不是嵌套條件。 – dreamweiver 2013-05-07 09:48:27

回答

3

我寧願返回包含錯誤的字段中的每一個驗證去,這樣你可以顯示哪些字段被錯誤地填寫了

function Validation() { 
    var submit_name = document.getElementById('txt_Name').value, 
     submit_surname = document.getElementById('txt_Surname').value, 
     submit_mobilenumber = document.getElementById('MobileNumber').value, 
     errors = []; 

    if(!checkName(submit_name)) { 
     errors.push({ 
      id: 'txt_Name', 
      message: 'Name is required' 
     }); 
    } 

    if(!checkSurname(submit_surname)) { 
     errors.push({ 
      id: 'txt_Surname', 
      message: 'Surname is required' 
     }); 
    } 

    if(!checkMobile(submit_mobilenumber)) { 
     errors.push({ 
      id: 'MobileNumber', 
      message: 'Mobile number is required' 
     }); 
    } 

    return errors; 
} 

var errors = Validation(); 

if(errors.length === 0) { 
    // No errors, do your stuff 
} else { 
    // Loop errors and do something 
    for(var i = 0; i < errors.length; i++) { 
     // can mark the element with document.getElementById(errors[i].id) 
     alert(errors[i].message); 
    } 
} 
+0

這改善了我的答案,爲每個元素添加一些錯誤消息。使用我的,你可以顯示像'請填寫所有輸入'或類似的東西。這兩種解決方案都不錯,只取決於你想要的特定程度。很好的夥伴+1 – DaGLiMiOuX 2013-05-07 10:04:07

+0

是的,這真的取決於開發者的需求。當我驗證表單時,我個人更喜歡這樣的東西。 – thebreiflabb 2013-05-07 10:14:36

2

程序將停止在第一個方法返回false,因爲所有這些都包含在彼此。因此,如果checkname()返回false,validation()將返回false,併爲其他函數相同。

// if false, validate() returns false. if true, checksurname() called 
if(checkName(submit_name)) 
{ 
    // if false, validate() returns false. if true, checkMobile() called 
    if(checkSurname(submit_surname)) 
    { 
     // if false, validate() returns false 
     if(checkMobile(submit_mobilenumber)) 
     { 
     } 
    } 
} 
return false; 

雖然作爲dreamweiver說,各個驗證效果會更好:

if(checkname()&&checkmobile()&&checksurname()) 
{ 
    return true; 
} 
else 
{ 
    return false; 
} 
2

一個簡單的解決方案(沒有忽略你的其他檢查功能)爲您的驗證應該是:

function Validation() 
{ 
    var booleanValidation = false; 
    var check_submit_name = checkName(document.getElementById('txt_Name').value); 
    var check_submit_surname = checkSurname(document.getElementById('txt_Surname').value); 
    var check_submit_mobilenumber = checkMobile(document.getElementById('MobileNumber').value); 

    if (check_submit_name === false || check_submit_surname === false || check_submit_mobilenumber === false) { 
     booleanValidaation = false; 
    } else if (check_submit_name === true && check_submit_surname === true && check_submit_mobilenumber === true) { 
     booleanValidaation = true; 
    } 

    return booleanValidation; 
} 
0

如果checkName(submit_name)將返回false,那麼

if(checkName(submit_name))將變爲if(false),因此條件將是錯誤的,因此if-condition代碼將不會執行。並將繼續執行。

這將適用於所有if條件。