2014-03-13 52 views
1

我有一個輸入字段驗證年齡和一個複選框,當它點擊它激活表單的#btn激活按鈕,我想改變我的代碼,所以只有#btn-activation按鈕當它們都是真的時候被激活。主動提交按鈕與兩個st

複選框:

$("#ChkTerms").prop('checked', false); 
    $("#ChkTerms").click(function() { 
     if ($('#ChkTerms').is(":checked")) { 
      $("#btn-activation").removeAttr('disabled'); 
     } else { 
      $("#btn-activation").attr("disabled", "disabled"); 
     } 
    }); 

這基本上會檢查是否#chkTerms被選中與否,並且如果它是它移除#BTN-激活按鈕「禁用」屬性。無論年齡驗證是否正確。

年齡驗證領域

 if (res != null) { 
       birth_date = new Date(res[1], res[2] - 1, res[3]); 
       birth_date.setFullYear(birth_date.getFullYear() + 18); 
       if (birth_date <= new Date()) { 
        $("#btn-activation").removeAttr('disabled'); 
        $("#SocialSecurityNumber").removeClass("input-validation-error"); 
       } else { 
        $("#SocialSecurityNumber").val("").attr("placeholder", "You need to be 18 years or older.").addClass("input-validation-error"); 
        $("#btn-activation").attr("disabled", "disabled"); 
       } 
      } else { 
       $("#SocialSecurityNumber").val("").attr("placeholder", "Invalid date").addClass("input-validation-error"); 
       $("#btn-activation").attr("disabled", "disabled"); 
      } 
    }); 

在這裏你可以看到我的IF語句,如果用戶是超過18它消除了 '禁用',如果不是它增加了 '禁​​用'

現在如果年齡是無效的,你可以檢查複選框,提交按鈕被激活,我只希望它激活時,這兩個語句都是真實的。

編輯:

 $("#ChkTerms").prop('checked', false); 
    $("#ChkTerms").click(function() { 
     if ($('#ChkTerms').is(":checked") && ValidateAge()) { 
      $("#btn-activation").removeAttr('disabled'); 
     } else { 
      $("#btn-activation").attr("disabled", "disabled"); 
     } 
    }); 

    function ValidateAge() { 
     $("#SocialSecurityNumber").attr("placeholder", "YYYY-MM-DD-XXXX").blur(function() { 
      var str = $('#SocialSecurityNumber').val(); 
      var res = /^([1-2]\d{3})\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])\-([0-9]{4})$/.exec(str); 
      var todays_date = new Date(); 
      var birth_date = null; 

      if (res != null) { 
       birth_date = new Date(res[1], res[2] - 1, res[3]); 
       birth_date.setFullYear(birth_date.getFullYear() + 18); 
       if (birth_date <= new Date()) { 
        // $("#btn-activation").removeAttr('disabled'); 
        $("#SocialSecurityNumber").removeClass("input-validation-error"); 
        return true; 
       } else { 
        $("#SocialSecurityNumber").val("").attr("placeholder", "You need to be 18 years or older.").addClass("input-validation-error"); 
        // $("#btn-activation").attr("disabled", "disabled"); 
        return false; 
       } 
      } else { 
       $("#SocialSecurityNumber").val("").attr("placeholder", "Invalid date").addClass("input-validation-error"); 
       // $("#btn-activation").attr("disabled", "disabled"); 
       return false; 
      } 
     }); 
    } 
    return ValidateAge() 
+0

你可以發佈html或小提琴嗎?哪個活動最後會執行? 'if(res!= null){'什麼是rec? –

回答

0

您可以創建驗證功能,檢查一切,並把它在每一個事件處理程序,如果一切正確的 - 改變的狀態。

function validate(res) { 
    var birth_date = new Date(res[1], res[2] - 1, res[3]); 
    var birth_date.setFullYear(birth_date.getFullYear() + 18); 

    return birth_date <= new Date() && $('#ChkTerms').is(":checked"); 
} 

if (validate(res)) { 
    $("#btn-activation").removeAttr('disabled'); 
} 
1

這裏有一個簡單的解決方案

把在功能上你的年齡驗證碼說ValidateAge(),即根據結果返回true或false,不啓用和禁用按鈕。 並更改複選框單擊代碼:

$("#ChkTerms").click(function() { 
     if ($('#ChkTerms').is(":checked") && ValidateAge()) { 
      $("#btn-activation").removeAttr('disabled'); 
     } else { 
      $("#btn-activation").attr("disabled", "disabled"); 
     } 
    }); 

UPDATE 我想改變你的代碼通過以下方式有點會爲你工作。

$(document).ready(function() { 

    $("#ChkTerms").click(function() { 
     if ($('#ChkTerms').is(":checked") && ValidateAge()) { 
      $("#btn-activation").removeAttr('disabled'); 
     } else { 
      $("#btn-activation").attr("disabled", "disabled"); 
     } 
    }); 
}); 

function ValidateAge() { 
var ret = false; 
    $("#SocialSecurityNumber").attr("placeholder", "YYYY-MM-DD-XXXX").blur(function() { 
     var str = $('#SocialSecurityNumber').val(); 
     var res = /^([1-2]\d{3})\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])\-([0-9]{4})$/.exec(str); 
     var todays_date = new Date(); 
     var birth_date = null; 
     if (res != null) { 
     birth_date = new Date(res[1], res[2] - 1, res[3]); 
     birth_date.setFullYear(birth_date.getFullYear() + 18); 
     if (birth_date <= new Date()) {    
      $("#SocialSecurityNumber").removeClass("input-validation-error"); 
      ret= true; 
     } else { 
      $("#SocialSecurityNumber").val("").attr("placeholder", "You need to be 18 years or older.").addClass("input-validation-error"); 
      ret = false; 
     } 
    } else { 
     $("#SocialSecurityNumber").val("").attr("placeholder", "Invalid date").addClass("input-validation-error"); 
     ret = false; 
    } 
    }); 
return ret; 
} 
+0

嗨,我試着圍繞函數ValidateAge()包裝我的匿名函數,並讓每個IF返回false或true - 並使用你的代碼,但無法讓它工作。我編輯了我的帖子 – rac

+0

用完整的代碼編輯了我的帖子。 – rac

+0

我無法理解你的代碼,我正在用一個例子更新我的答案。 –