当用户输入时,如何提醒用户输入类型的文本检测到一个点

How to alert user that input type text detects a dot when user input?

本文关键字:用户 输入 一个 检测 何提醒 类型 文本      更新时间:2023-09-26

我有这个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>

我希望span id="checkID"在输入字段检测到keyup上的点时出现。我尝试使用indexOf((,但span标记不会出现。有人能帮我吗?

我已经更新了你的代码。请在下面的fiddle链接中找到更新的代码。只添加了一个功能,并更新了keyup和keypress事件。

$(function() {
    $('#parentID').on('keypress keyup', '#childID', function(e) {
        if(isNumber(e)){
            $("#checkID").html("");
        } else{
            //e.preventDefault();
            $("#checkID").html("ID number must contain numbers only");
            $("#checkID").css("color", "orange");
        }
    });
});

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode <= 46 || charCode > 57)) {
        return false;
    }
    return true;
}

参见演示

它显示错误消息。如果您不想允许使用其他字符,请取消注释//e.preventDefault((;

尝试这个检查它实时

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<style type="text/css">
</style>
<body>
first name :<input type="text" name="firstname" class="tfield" id="myinput">
</body>
<script type="text/javascript">
$(document).ready(function(){
	$("#myinput").on('change keyup keydown',function(){
		var thetext = $(this).val();
		if (thetext.indexOf('.') != -1) 
		{
			alert("contains dot sign")
		}
		else
		{
			//no need
		}
	});
});
</script>

$("#childID").keyup(function(){
    if ($("#childID").val().indexOf('.') > -1) {
     alert('dot');
     }

});

工作演示1

工作演示2

只需编写一个javascript函数即可检查charCode。dot的关键字Code是46。所以javascript函数可能喜欢这个

$('#childID').keypress(function (e) {
   //if the letter is dot
   if (e.which == 46) {
       //display alert error message
       alert("you cant enter dot");
       $(this).focus();
       return false;
   }
});

这将不允许用户在文本字段中键入句点
演示:

<!DOCTYPE html>
<html>
<title></title>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<style type="text/css">
</style>
<body>
Enter text :<input type="text" name="childID" id="childID">
</body>
<script type="text/javascript">
$(document).ready(function(){
	$('#childID').keypress(function (e) {
        //if the letter is dot
        if (e.which == 46) {
            //display alert error message
            alert("you cant enter dot");
            $(this).focus();
            return false;
        }
  });
});
</script>