Want to limit korean and chinese
Asked Answered
R

2

9

I need to limit input box content based on lang. enter.

For example:-

If a string with Korean characters is input, then the number of permitted characters is 8. If a string with Chinese characters is input, the number of permitted characters is 5. If that with English, then 12 characters are permitted.

My code is working well for English characters in IE, Firefox and Chrome. However, this code is not working as expected for Korean and Chinese characters. My code always cuts the length of string to 2 even if i increase the valid length. Please suggest some solution as soon as possible.

I am pasting my code for checking.

<!DOCTYPE html>
<html>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script>
            document.onkeydown=function() {


                    var text = $('#lang').val();
                    var hangul = new RegExp("[\u1100-\u11FF|\u3130-\u318F|\uA960-\uA97F|\uAC00-\uD7AF|\uD7B0-\uD7FF]"); 
                    var china = new RegExp("[\u4E00-\u9FFF|\u2FF0-\u2FFF|\u31C0-\u31EF|\u3200-\u9FBF|\uF900-\uFAFF]"); 

                // alert(hangul.test(text));
                    if(hangul.test(text))
                    {
                        limit = 8; 
                        //console.log("korean");
                        limitCharacters('lang', limit , text);

                    }else if(china.test(text))
                    {
                        limit = 5; 
                        //console.log("china");
                        limitCharacters('lang', limit , text);

                    }else {
                        limit = 11; 
                        limitCharacters('lang', limit , text);

                    }
         };

        function limitCharacters(textid, limit, text)
        {
          //alert('here in limit funt.');
          var textlength = text.length;
           //alert(textlength);
          if(textlength > limit )
           {
             $('#'+textid).val(text.substr(0,limit));
             return false;
           }else {

            $('#'+textid).val(text);
            $('#txt').html(text);
            return true;
           }

        } 



    </script>
<body>
    <input type="text" id="lang" />

</body>
</html>
Reparable answered 12/11, 2013 at 5:38 Comment(5)
So, it always cuts the length of a non-English string to two characters, even if you raise limit to, say, 100? What happens if you type eight English letters and then a Chinese character?Vodka
I'm not sure why you say your code does not work? I cut/pasted your code example and used the chinese character '戴'. It limited me to 6 chars. When I used english, it limited me to 12. I used IE 9.Harlene
Yes @user1618143, @Andrew-openGeoCode actually it works fine if I test it using alert or console.log. Even if I copy and paste string in any language to the input box it works fine... but writing in input box with Korean characters not working. The problem is I couldn't able to make out why it is cutting Korean characters limit.Reparable
Could you put up a JSFiddle so we can poke at it? Poking things usually helps.Vodka
You can access the user's browser language with navigator.language and use that in your input logicReminiscence
R
1

I solved this issue and now it is working fine for me. As per my understanding substring is not supported by IE.

<html>
        <title> test</title>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script type="text/javascript" src="http://css-browser-selector.googlecode.com/git/css_browser_selector.js"></script>
        <script src="./beta.fix.js"></script>
        <script>
             var keyFix = new beta.fix('lang');

            jQuery(document).ready( function($) {
                    jQuery('#lang').bind('keyup', checklang);

                });     

              function checklang() {
                        var textid = jQuery(this).attr("id");
                        var text = jQuery(this).val();
                        var hangul = new RegExp("[\u1100-\u11FF|\u3130-\u318F|\uA960-\uA97F|\uAC00-\uD7AF|\uD7B0-\uD7FF]"); 
                        var china = new RegExp("[\u4E00-\u9FFF|\u2FF0-\u2FFF|\u31C0-\u31EF|\u3200-\u9FBF|\uF900-\uFAFF]"); 

                       // alert(hangul.test(text));
                        if(china.test(text))
                        {
                            limit = 5;
                            console.log("chiness"); 
                        }else if(hangul.test(text))
                        {
                            limit = 8; 
                            console.log("korean"); 
                        }else {
                            limit = 11; 
                            console.log("english"); 
                        }
                        jQuery('#'+textid).attr("maxlength", limit);
             };
        </script>
    <body>
        <input type="text" id="lang" size="100" />

    </body>
    </html>
Reparable answered 15/11, 2013 at 5:17 Comment(0)
R
0

Can you try this:

var hangul = new RegExp("[\u1100-\u11FF|\u3130-\u318F|\uA960-\uA97F|\uAC00-\uD7AF|\uD7B0-\uD7FF]");
var china = new RegExp("[\u4E00-\u9FFF|\u2FF0-\u2FFF|\u31C0-\u31EF|\u3200-\u9FBF|\uF900-\uFAFF]");

$("#lang").on("keypress keyup", function () {
    var that = $(this);
    var text = that.val();

    if (china.test(text)) {
        limit = 5;

    } else if (hangul.test(text)) {
        limit = 8;

    } else {
        limit = 11;
    }
    that.attr("maxlength", limit);
    if (text.length > limit) that.val(text.substring(0, limit))
});

also on http://jsfiddle.net/sWPeN/1/

Reddin answered 14/11, 2013 at 21:33 Comment(1)
Thanks @Reddin for showing me other direction. Although this code is not working on IE but helpful for me..Reparable

© 2022 - 2024 — McMap. All rights reserved.