How to allow only arabic characters in form textbox using jquery

how to allow only arabic characters in form textbox using jquery

I tried with this, but the arrows do not allow returning from the last line

Let me know if there is, or edit my code or give a new code

<!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>
+4
source share
1 answer

Like Unicode 6.1, the Arabic script is contained in the following blocks (from Regular Expression for Arabic )

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)

Use a regular expression for a text field with any structure used.

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;
});

Here is jsfiddle

+7
source

Source: https://habr.com/ru/post/1619862/


All Articles