限制文本控件输入数据格式
1 // = 比如只能输入数字、字母、数字和字母 2 3 <script type="text/javascript"> 4 UM.getEditor('myEditor1') 5 6 $(function () { 7 8 // ---------------------------------------------------------------------- 9 // <summary> 10 // 限制只能输入字母 11 // </summary> 12 // ---------------------------------------------------------------------- 13 $.fn.onlyAlpha = function () { 14 $(this).keypress(function (event) { 15 var eventObj = event || e; 16 var keyCode = eventObj.keyCode || eventObj.which; 17 if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) 18 return true; 19 else 20 return false; 21 }).focus(function () { 22 this.style.imeMode = 'disabled'; 23 }).bind("paste", function () { 24 var clipboard = window.clipboardData.getData("Text"); 25 if (/^[a-zA-Z]+$/.test(clipboard)) 26 return true; 27 else 28 return false; 29 }); 30 }; 31 32 // ---------------------------------------------------------------------- 33 // <summary> 34 // 限制只能输入数字 35 // </summary> 36 // ---------------------------------------------------------------------- 37 $.fn.onlyNum = function () { 38 $(this).keypress(function (event) { 39 var eventObj = event || e; 40 var keyCode = eventObj.keyCode || eventObj.which; 41 if ((keyCode >= 48 && keyCode <= 57)) 42 return true; 43 else 44 return false; 45 }).focus(function () { 46 //禁用输入法 47 this.style.imeMode = 'disabled'; 48 }).bind("paste", function () { 49 //获取剪切板的内容 50 var clipboard = window.clipboardData.getData("Text"); 51 if (/^\d+$/.test(clipboard)) 52 return true; 53 else 54 return false; 55 }); 56 }; 57 58 // ---------------------------------------------------------------------- 59 // <summary> 60 // 限制只能输入数字和字母 61 // </summary> 62 // ---------------------------------------------------------------------- 63 $.fn.onlyNumAlpha = function () { 64 $(this).keypress(function (event) { 65 var eventObj = event || e; 66 var keyCode = eventObj.keyCode || eventObj.which; 67 if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) 68 return true; 69 else 70 return false; 71 }).focus(function () { 72 this.style.imeMode = 'disabled'; 73 }).bind("paste", function () { 74 var clipboard = window.clipboardData.getData("Text"); 75 if (/^(\d|[a-zA-Z])+$/.test(clipboard)) 76 return true; 77 else 78 return false; 79 }); 80 }; 81 82 // 限制使用了onlyNum类样式的控件只能输入数字 83 $(".onlyNum").onlyNum(); 84 //限制使用了onlyAlpha类样式的控件只能输入字母 85 $(".onlyAlpha").onlyAlpha(); 86 // 限制使用了onlyNumAlpha类样式的控件只能输入数字和字母 87 $(".onlyNumAlpha").onlyNumAlpha(); 88 }); 89 </script>