简体   繁体   English

如何检查是否有人在输入文字上添加了4个字母或更多?

[英]How to check if someone has added 4 letters or more on input type text?

With this script bellow made a ajax call to my file veriUSPF.php in this file i have a SELECT query to check if the username is available. 使用此脚本,波纹管对我的文件veriUSPF.php进行了ajax调用,我有一个SELECT查询来检查用户名是否可用。 This script bellow is executed onBlur , but i want it to execute the ajax call only if someone added 4 letters or more on the input, is this possible? 该脚本在onBlur上执行,但是我希望它仅在有人在输入中添加4个或更多字母时才执行ajax调用,这可能吗?

html: 的HTML:

<input id="UsName" type="text" minlength="4" maxlength="17" autocomplete="off" onBlur="checkAvailability()" value="<?php echo htmlentities($username, \ENT_QUOTES, 'UTF-8', false); ?>" name="changeUsernamePF">
<span id="user-availability-status"></span>

script: 脚本:

function checkAvailability() {
    jQuery.ajax({
        url: siteURL + "/veriUSPF.php",
        data: "changeUsernamePF=" + $("#UsName").val(),
        type: "POST",
        success: function(a) {
            $("#user-availability-status").html(a)
        }
    })
}

You may use input event: 您可以使用输入事件:

 $('#UsName').on('input', function(e) { if (this.value.length>=4) { console.log('call ajax now'); } }).trigger('input'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="UsName" type="text" minlength="4" maxlength="17" autocomplete="off" onBlur="checkAvailability()" value="" name="changeUsernamePF"> <span id="user-availability-status"></span> 

Using an inline event you can write: 使用内联事件,您可以编写:

 function checkAvailability(e, ele) { if (ele.value.length>=4) { console.log('call ajax now'); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="UsName" type="text" minlength="4" maxlength="17" autocomplete="off" oninput="checkAvailability(event, this)" value="" name="changeUsernamePF"> <span id="user-availability-status"></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM