Home > Web Front-end > JS Tutorial > 3 Jquery ways to limit the text box to only numbers and letters_jquery

3 Jquery ways to limit the text box to only numbers and letters_jquery

WBOY
Release: 2016-05-16 16:29:07
Original
1811 people have browsed it

onlyNum(), onlyAlpha() and onlyNumAlpha() 3 Jquery extension methods

number.js

Copy code The code is as follows:

//------------------------------------------------ -----------------------
//
// Limit input to numbers only
//

//------------------------------------------------ -----------------------
$.fn.onlyNum = function () {
$(this).keypress(function (event) {
        var eventObj = event || e;
          var keyCode = eventObj.keyCode || eventObj.which;
If ((keyCode >= 48 && keyCode <= 57))
             return true;
        else
              return false;
}).focus(function () {
//Disable input method
This.style.imeMode = 'disabled';
}).bind("paste", function () {
//Get the contents of the clipboard
      var clipboard = window.clipboardData.getData("Text");
If (/^d $/.test(clipboard))
             return true;
        else
              return false;
});
};

letter.js

Copy code The code is as follows:

//------------------------------------------------ -----------------------
//
//Limit the input to only letters
//

//------------------------------------------------ -----------------------
$.fn.onlyAlpha = function () {
$(this).keypress(function (event) {
        var eventObj = event || e;
          var keyCode = eventObj.keyCode || eventObj.which;
If ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
             return true;
        else
              return false;
}).focus(function () {
This.style.imeMode = 'disabled';
}).bind("paste", function () {
      var clipboard = window.clipboardData.getData("Text");
If (/^[a-zA-Z] $/.test(clipboard))
             return true;
        else
              return false;
});
};

number_letter.js

Copy code The code is as follows:

// ----------------------------------------------------------------------
//
// 限制只能输入数字和字母
//

// ----------------------------------------------------------------------
$.fn.onlyNumAlpha = function () {
    $(this).keypress(function (event) {
        var eventObj = event || e;
        var keyCode = eventObj.keyCode || eventObj.which;
        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
            return true;
        else
            return false;
    }).focus(function () {
        this.style.imeMode = 'disabled';
    }).bind("paste", function () {
        var clipboard = window.clipboardData.getData("Text");
        if (/^(d|[a-zA-Z]) $/.test(clipboard))
            return true;
        else
            return false;
    });
};

use.js

复制代码 代码如下:

$(function () {
    // 限制使用了onlyNum类样式的控件只能输入数字
    $(".onlyNum").onlyNum();
    //限制使用了onlyAlpha类样式的控件只能输入字母
    $(".onlyAlpha").onlyAlpha();
    // 限制使用了onlyNumAlpha类样式的控件只能输入数字和字母
    $(".onlyNumAlpha").onlyNumAlpha();

以上方法均可实现项目要求,大家根据自己的具体需求自由选择吧

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template