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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:12:17
Original
1718 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:
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template