2014-08-28 42 views

回答

1

HTML:用於以下領域爲

碼 -

<input name="invoice_price" class="form-control"> 

<div id="div1" style="color:red"></div> 

jQuery的: -

$(document).ready(function(){ 
    $("input[name='invoice_price']").on('blur keyup',function(){ 
    if($(this).val()=='0') 
    { 
     $("#div1").html('0 value not allowed'); 
     $(this).val(''); 
    } 
    else 
    { 
     $("#div1").html(''); 
    } 
    }); 
}); 

小提琴鏈接: - http://jsfiddle.net/0mat1amf/1/


由於提問者問了另一個問題在評論部分(即用戶只能輸入最多三個位),然後我在這裏加入更多的代碼:

<input name="invoice_price" class="form-control" type="text" maxlength="3"> 
+0

準確地說,他的投入不是由'invoice_price的id'鑑定,其名字'attribute'元素。 – Gabor 2014-08-28 10:30:59

+0

@ Gabor..plz查看最新的答案 – 2014-08-28 10:31:52

+0

@SalmanKhan ...我認爲這就是你想要的.. – 2014-08-28 10:43:27

0

HTML:

<input name="invoice_price" class="form-control" 
    onblur="(this.value == 0) ? (this.value = '') : ''" /> 

Demo

這將確保用戶不能輸入零​​值。無需顯示錯誤!

+0

客戶:「哦,我的上帝,我想輸入0.5但我的鍵盤不工作」 – Kyborek 2014-08-28 10:42:10

+0

@Kyborek你走了!考慮撤銷倒票。 – 2014-08-28 10:48:30

0

這是,如果你想堆起來的錯誤信息,你可以用什麼:

$(document).ready(function(){ 
    $("input[name=invoice_price]").change(function(){ 
    if ($(this).val()==0) 
    $(this).after("<div>Error message</div>"); 
    }); 
}); 

每次輸入被改變,是錯誤的時間輸入元素之後它會創建的錯誤消息。相反,你可能希望有預定義的div和只更改喜歡它的內容:

<input name="invoice_price" class="form-control"> 
<div></div> 
$(document).ready(function(){ 
    $("input[name=invoice_price]").change(function(){ 
    if ($(this).val()==0) 
    $(this).next().html("Error message"); 
    }); 
}); 
+0

它真的很棒..感謝它..! – 2014-08-28 11:13:05

+0

如果答案適用於您,並且您什麼都不需要,請將答案標記爲已接受,這樣當您不再需要他們的幫助時(不會再有人會來幫助您)(您將浪費時間):http ://stackoverflow.com/help/someone-answers – Kyborek 2014-08-28 13:01:53