Question

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>
Était-ce utile?

La solution

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>

Autres conseils

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/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top