javascript - regular expression not working in IE8 -
i'm going test inserted character if rtl or ltr , used code:
function checkrtl(s) { var ltrchars = 'a-za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0800- \u1fff' + '\u2c00-\ufb1c\ufdfe-\ufe6f\ufefd-\uffff', rtlchars = '\u0591-\u07ff\ufb1d-\ufdfd\ufe70-\ufefc', rtldircheck = new regexp('^[^' + ltrchars + ']*[' + rtlchars + ']'); return rtldircheck.test(s); } ; function checkspecialchars(s) { var schars = '0-9`~!@#$%^&*()_+\\-=\\|\\[\\]{};\'":,.<>/', checkchar = new regexp('^[' + schars + ']+'); return checkchar.test(s); } var input = $('#password').on('keypress', keypress); function keypress(e) { settimeout(function () { var isrtl = checkrtl(string.fromcharcode(e.charcode)); var isspecial = checkspecialchars(string.fromcharcode(e.charcode)); var dir = isrtl ? 'rtl' : 'ltr'; if(dir=='rtl'){ document.getelementbyid("langdir").innerhtml="<img src='../img /signup_images/att.png'>hello!"; $("#langdir").css("display","block"); } else { document.getelementbyid("langdir").innerhtml=""; $("#langdir").css("display","none"); } }, 100); }
this code working in ie 9, 10, chrome , firefox. it's not working in ie8 after debugging i've found out line:
rtldircheck = new regexp('^[^' + ltrchars + ']*[' + rtlchars + ']'); return rtldircheck.test(s);
always returns false. wrong that?
first of can detect nothing wrong regex. following html ie 7 10 tells me result equals true (i using ie 10.0 in backwards compatibility modes).
<html> <body> <script> var ltrchars = 'a-za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0800-\u1fff' + '\u2c00-\ufb1c\ufdfe-\ufe6f\ufefd-\uffff', rtlchars = '\u0591-\u07ff\ufb1d-\ufdfd\ufe70-\ufefc', rtldircheck = new regexp('^[^' + ltrchars + ']*[' + rtlchars + ']'); alert("result = "+ rtldircheck.test('\u0591') ); </script> </body> </html>
the problem not function. input:
string.fromcharcode(e.charcode)
ie doesn't set e.charcode, used e.keycode. e.charcode used webkit browsers. have @ chapter 3 of excellent breakdown of differences between keyboard events in different browser. suggest using:
string.fromcharcode(e.which || e.keycode)
Comments
Post a Comment