我有這個HTML和jQuery代碼,提醒用戶只有數字輸入。 我想在用戶輸入點''時提醒用戶。在文本字段中。如何在用戶輸入時提醒用戶輸入類型文本檢測到點?
<html>
<body>
<div class="form-group" id="parentID">
<input class="form-control" placeholder="ID Number (10 max numbers)" id="childID" name="idnumber" type="text" maxlength="10" required autofocus>
<span id="checkID"></span>
</div>
</body>
<script>
var ID = $("#childID").val();
if (ID.indexOf(".") != -1) {
$("#checkID").html("ID number must contain numbers only");
$("#checkID").css("color", "orange");
}
$(document).ready(function() {
$("#childID").keyup(checkPasswordMatch);
});
//In this function it will disable special characters in user input but still accepts the '.' and '>' character type, what keycode is it to disable that 2 character?
$(function() {
$('#parentID').on('keydown', '#childID', function(e) {-1 !== $.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) || /65|67|86|88/.test(e.keyCode) && (!0 === e.ctrlKey || !0 === e.metaKey) || 35 <= e.keyCode && 40 >= e.keyCode || (e.shiftKey || 48 > e.keyCode || 57 < e.keyCode) && (96 > e.keyCode || 105 < e.keyCode) && e.preventDefault() });
})
</script>
</html>
我希望當輸入字段檢測到keyup上的點時出現span id =「checkID」。我試過使用indexOf(),但span標籤不會出現。誰能幫我?
這段代碼工作得很好。謝謝 –
@laurencekeithalbano:太好了!看看這個演示https://jsfiddle.net/amarsingh/2vjoh4xb/1/ –
@laurencekeithalbano:你看到我的第二個演示?如果用戶繼續使用圓點打字,警告仍然存在。警報消息始終顯示爲紅色。但是,如果用戶繼續使用點號打字,則您接受的答案在該警報消息中會消失。你真的覺得答案更正確嗎? –