用于AlphaNumeric和大写字母的Javascript

时间:2014-02-10 14:04:27

标签: javascript jquery

我为AlphaNumeric编码了javascript。此外,如果用户键入小写,则转换为大写。从小写转换为大写obj.value = String.fromCharCode(key).toUppercase();不能正常工作。请帮我整理一下。提前谢谢..

function isAlphaNumeric(e, obj, index) {
     var key;
     if (window.event) {
         key = window.event.keyCode; //IE
         obj.value = String.fromCharCode(key).toUpperCase();
     } else {
         key = e.which; //firefox
         obj.value = String.fromCharCode(key).toUpperCase();
     }
     if (!((key > 64 && key <= 90) || (key > 96 && key <= 122) || (key > 47 && key <= 57) || (key == 8) || (key == 0) || (key == 127))) {
         alert(" Enter only Alpha-Numeric value in this field. ");
         setTimeout(function () {
             clearField(obj)
         }, 500);
         return false;
     }
 }

2 个答案:

答案 0 :(得分:0)

您应该使用.toUpperCase()而不是.toUppercase(),因为Javascript区分大小写。 http://www.w3schools.com/jsref/jsref_touppercase.asp

答案 1 :(得分:0)

Mate看看我的回答:

   $('textarea').bind('keydown keypress keyup', function (e) {
       var replacedText = $(this).val().replace(/[^A-Z0-9]/g, function (match) {
           if (match != undefined) {
               return match.toUpperCase();
           }
       });
       $(this).val(replacedText);
   });

以下是DEMO