如何使用jquery

时间:2015-12-14 11:20:18

标签: javascript jquery

如何使用jquery

仅允许表单文本字段中的阿拉伯字符

我试过这个,但箭头不允许从最后一行回来

让我知道是否有任何或编辑我的代码或提供任何新代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Character Filtering</title>
        <script type="text/javascript">

            function CheckArabicOnly(field) {
                var sNewVal = "";
                var sFieldVal = field.value;

                for (var i = 0; i < sFieldVal.length; i++) {
                    var ch = sFieldVal.charAt(i),
                        c  = ch.charCodeAt(0);

                    if (field.keyCode = '38') {
                    }
                    else if (field.keyCode = '40') {
                        // down arrow
                    }
                    else if (field.keyCode = '37') {
                    }
                    else if (field.keyCode = '39') {
                       // right arrow
                    }

                    if (c < 1536 || c > 1791) {
                        // Discard
                    }
                    else {
                        sNewVal += ch;
                    }
                }

                field.value = sNewVal;
            }
        </script>
    </head>
    <body>
        Arabic Only (onchange):
        <input type="text"
               id="txtArabic"
               onchange="CheckArabicOnly(this);"
               onkeypress="CheckArabicOnly(this);"
               onkeyup="CheckArabicOnly(this);"
               onpaste="CheckArabicOnly(this);"  />
        <br />
    </body>
</html>

1 个答案:

答案 0 :(得分:8)

从Unicode 6.1开始,阿拉伯语脚本包含在以下块中(来自 Regular Expression For Arabic Language

Arabic (0600—06FF, 225 characters)
Arabic Supplement (0750—077F, 48 characters)
Arabic Extended-A (08A0—08FF, 39 characters)
Arabic Presentation Forms-A (FB50—FDFF, 608 characters)
Arabic Presentation Forms-B (FE70—FEFF, 140 characters)
Rumi Numeral Symbols (10E60—10E7F, 31 characters)
Arabic Mathematical Alphabetic Symbols (1EE00—1EEFF, 143 characters)

对您的文本字段使用正则表达式验证,无论您使用何种框架。

var isArabic = /^([\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufbc1]|[\ufbd3-\ufd3f]|[\ufd50-\ufd8f]|[\ufd92-\ufdc7]|[\ufe70-\ufefc]|[\ufdf0-\ufdfd])*$/g;

// Solution 1: This will add an error class when the input does not match
$("#txtArabic1").change(function() {
    $(this).toggleClass("has-error", !isArabic.test(this.value));
})

// Solution 2: This will not let the user type chars that are not arabic
// but seem to fail on my browser... I don't quite get how does 
// javascript splits the string.
$("#txtArabic2").bind('keyup', function(e) {
    var filterFn = isArabic.test.bind(isArabic),
        newValue = this.value.split('').filter(filterFn).join('');

    if (this.value != newValue)
        this.value = newValue;
});

这是jsfiddle