2016-10-14 73 views
-4

如何防止輸入類型數量未定義值。舉例如下:如何防止輸入字段無效輸入

Image example

所以如何防止輸入正負?

+1

http://stackoverflow.com/questions/8282266/how-to-prevent-invalid-characters- from-being-typed-into-input-fields – RST

+2

使用'type =「text」'with'pattern =「[0-9] *」'或'pattern =「( - ?[0-9] +)? 「(甚至....只需編寫你需要驗證的正則表達式。) –

+0

表單驗證。 – Epodax

回答

0

塊 '+' 和 ' - ' 按鍵上的按鍵

<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<script> 
 
$(document).ready(function(){ 
 

 
}); 
 
</script> 
 
</head> 
 
<body> 
 
<div class="parent"> 
 
<input type="number" onkeypress='return event.charCode != 43 && event.charCode != 45'></input> 
 
</body> 
 
</html>

+0

如何阻止只是加鍵和按鍵事件mines鍵? – kamal

+0

block charCode 43(+)and 45( - ) – Aswathy

+0

code updated :) – Aswathy

0

你必須寫一些JavaScript代碼來實現你的目標。

document.querySelector("input").addEventListener("keypress", function (evt) { 
    if (evt.which < 48 || evt.which > 57) 
    { 
     evt.preventDefault(); 
    } 
}); 

按鍵會觸發輸入元素,並將氣泡寫入文檔。