如何将字段限制为仅使用4个数字字符作为输入并在div中获取警报

时间:2016-05-13 06:44:56

标签: javascript jquery html

我想要一个文本字段只能将数字作为一些控制键,而数字应该恰好是四位数。我的代码如下:

<div id="main" role="main">
                Input a 4-digit: <input type="text" class="validateYearTextBox" />
            </div>
<div id="alert"></div>

function checkValidInput() {
    $(".validateYearTextBox").keydown(function(event) {       
        if (!((event.keyCode == 46 || 
            event.keyCode == 8  || 
            event.keyCode == 37 || 
            event.keyCode == 39 || 
            event.keyCode == 9) || 
            $(this).val().length < 4 &&
            ((event.keyCode >= 48 && event.keyCode <= 57) ||
            (event.keyCode >= 96 && event.keyCode <= 105)))) {
                        // Stop the event
                        event.preventDefault();
                        return false;                       
            }
    });
}
$(document).ready(function() {

    checkValidInput();
});

当我在文本框中输入4位数时,警报应在div中显示为“有效”

这是Fiddle

5 个答案:

答案 0 :(得分:3)

试试这个:

Jsfiddle:https://jsfiddle.net/jz1ra36d/

只需添加:

$(".validateYearTextBox").keyup(function(event) {    
  if( $(this).val().length == 4){
     $("#alert").text("valid")
   } else {
     $("#alert").text("")
   }
});

答案 1 :(得分:2)

JSFiddle

请尝试以下操作:

<div id="main" role="main">
    Input a 4-digit: <input type="text" class="validateYearTextBox" />
</div>
<div id="alert"></div>

<script>
function checkValidInput() {
    $(".validateYearTextBox").keydown(function(event) {       
        if (!((event.keyCode == 46 || 
            event.keyCode == 8  || 
            event.keyCode == 37 || 
            event.keyCode == 39 || 
            event.keyCode == 9) || 
            $(this).val().length < 4 &&
            ((event.keyCode >= 48 && event.keyCode <= 57) ||
            (event.keyCode >= 96 && event.keyCode <= 105)))) {
                        // Stop the event
                        event.preventDefault();
                        return false;                       
            }

            // count number of characters entered
            var cs = $('.validateYearTextBox').val().length;
            if(cs == 3)
            {
                $('#alert').html('valid'); 
            }
            else
            {
                $('#alert').html(''); 
            }
    });
}
$(document).ready(function() {

    checkValidInput();
});
</script>

答案 2 :(得分:0)

您可以使用正则表达式并更改为keyup事件:

function checkValidInput() {
  var re = /\d{4}/g,
    dt = $('.validateYearTextBox'),
    msg;
  msg = re.test(dt.val()) ? "Valid" : "Invalid";
  $('#alert').text(msg);
}

$(".validateYearTextBox").keyup(checkValidInput);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main" role="main">
  Input a 4-digit:
  <input type="text" class="validateYearTextBox" />
</div>
<div id="alert"></div>

答案 3 :(得分:0)

我会做这样的事情。

function checkValidInput() {
    $(".validateYearTextBox").keydown(function(event) {    

       if( $(this).val().length == 3){
            $('#alert').html('<p>SUCCESS</p>');
       } 
        if (!((event.keyCode == 46 || 
            event.keyCode == 8  || 
            event.keyCode == 37 || 
            event.keyCode == 39 || 
            event.keyCode == 9) || 
            $(this).val().length < 4 &&
            ((event.keyCode >= 48 && event.keyCode <= 57) ||
            (event.keyCode >= 96 && event.keyCode <= 105)))) {
                        // Stop the event
                        event.preventDefault();
                        return false;                       
            }
    });
}
$(document).ready(function() {

    checkValidInput();
});

答案 4 :(得分:0)

在将onblur =“checkValidInput”id =“validateYearTextBox”添加到输入标记后尝试这个简单的脚本

function checkValidInput(){
var x,text;
x=document.getelementbyid("validateYearTextBox").value;
if(x==4)
text="Valid";
else
text="Not valid";
}
document.getelementbyid("alert").innerHTML=text;