Home > Web Front-end > JS Tutorial > JS restricts the text box to only input numbers and letters_javascript skills

JS restricts the text box to only input numbers and letters_javascript skills

WBOY
Release: 2016-05-16 16:12:17
Original
1676 people have browsed it

Limit input to numbers only

Copy code The code is as follows:

//------------------------------------------------ -----------------------
//
// Limit input to numbers only
// demo: $(".onlyNum").onlyNum(); restricts controls using the onlyNum class style to only input numbers
//

//------------------------------------------------ -----------------------
$.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;
});
};

Limit input to letters only

Copy code The code is as follows:

//------------------------------------------------ -----------------------
//
//Limit the input to only letters
// demo: $(".onlyAlpha").onlyAlpha(); restricts controls using the onlyNumAlpha class style to only input numbers and 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;
});
};

Limit input to numbers and letters only

Copy code The code is as follows:

//------------------------------------------------ -----------------------
//
//Limit input to numbers and letters only
// demo: $(".onlyNumAlpha").onlyNumAlpha(); restricts controls using the onlyNumAlpha class style to only input numbers and letters
//

//------------------------------------------------ -----------------------
$.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;
});
};
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