2014-02-23 37 views
0

哪裏是錯誤爲什麼DIV不會顯示,我想要做的是檢查的數字是不是空的,值越低,有13jQuery驗證,如果其空,數字或值高於13

JS

(function($) {  
if($('#month-holder').length){ 
$('#month-holder').on("blur", function(e){ 
    e.preventDefault(); 
    var month = $("#month-holder").val(); 
    if (typeof month != "undefined"){ 
     if(!$.isNumeric(month)){ 
      if (parseInt(month) > 13) { 
       $('#month-error').fadeIn('slow'); 
       $('#month-holder').addClass("invalid"); 
      } 
     } 
    } 
    else 
    { 
     $('#month-error').fadeOut('slow'); 
     $('#month-holder').removeClass("invalid"); 
    } 
}); 
} 
})(jQuery); 

CSS

input.invalid {  
border: 1px solid red;  
-moz-box-shadow: 0 0 2px 1px #e4e4e4; 
-webkit-box-shadow: 0 0 2px 1px #e4e4e4; 
box-shadow: 0 0 2px 1px #e4e4e4; 
} 
#month-error{ 
display: none; 
} 
.error{ color: #f00; padding: 5px 0 0} 

HTML

<input type="text" name="month-holder" id="month-holder" class="input-txt width-65" placeholder="MM" maxlength="2" pattern="\d*" x-autocompletetype="month-holder" required autocomplete="off"> 
<div id="month-error" class="error clearfix">Error</div> 

任何一個可以幫我請

回答

0

改變你的conditon

if(!$.isNumeric(month)){ 

if($.isNumeric(month)){ 

當前的implementaion將無法正常工作,如果你第一次輸入無效數據,然後將其刪除並重新輸入有效數據它不會刪除錯誤消息。

只需你可以做到這一點是這樣的:

(function($) {  
if($('#month-holder').length){ 
$('#month-holder').on("blur", function(e){ 
    e.preventDefault(); 
    var month = $("#month-holder").val(); 
    if ($.isNumeric(month) && parseInt(month) < 13) { 
       $('#month-error').fadeOut('slow'); 
       $('#month-holder').removeClass("invalid"); 

     } 
     else 
     { 
     $('#month-error').fadeIn('slow'); 
     $('#month-holder').addClass("invalid"); 
     } 
}); 
} 
})(jQuery); 

演示:http://jsfiddle.net/a6NJk/661/

+0

@Digital注:你測試它? –

0
if($.isNumeric(month) && parseInt(month) > 13){ 
      $('#month-error').fadeIn('slow'); 
      $('#month-holder').addClass("invalid"); 
     } 

     else 
     { 
      $('#month-error').fadeOut('slow'); 
      $('#month-holder').removeClass("invalid"); 
     } 
+0

你的邏輯錯誤! 'parseInt(month)> 13'? –

相關問題