onlynum(),onlyalpha()和onlynumalpha()3个jquery扩展方法
number.js
复制代码 代码如下:
// ----------------------------------------------------------------------
//
// 限制只能输入数字
//
// ----------------------------------------------------------------------
$.fn.onlynum = function () {
$(this).keypress(function (event) {
var eventobj = event || e;
var keycode = eventobj.keycode || eventobj.which;
if ((keycode >= 48 && keycode return true;
else
return false;
}).focus(function () {
//禁用输入法
this.style.imemode = 'disabled';
}).bind(paste, function () {
//获取剪切板的内容
var clipboard = window.clipboarddata.getdata(text);
if (/^\d+$/.test(clipboard))
return true;
else
return false;
});
};
letter.js
复制代码 代码如下:
// ----------------------------------------------------------------------
//
// 限制只能输入字母
//
// ----------------------------------------------------------------------
$.fn.onlyalpha = function () {
$(this).keypress(function (event) {
var eventobj = event || e;
var keycode = eventobj.keycode || eventobj.which;
if ((keycode >= 65 && keycode = 97 && keycode 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
复制代码 代码如下:
// ----------------------------------------------------------------------
//
// 限制只能输入数字和字母
//
// ----------------------------------------------------------------------
$.fn.onlynumalpha = function () {
$(this).keypress(function (event) {
var eventobj = event || e;
var keycode = eventobj.keycode || eventobj.which;
if ((keycode >= 48 && keycode = 65 && keycode = 97 && keycode 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();
以上方法均可实现项目要求,大家根据自己的具体需求自由选择吧