javascript - Strange .replace() behaviour in Chrome browser -


<div>     <input type="text" class="allownumericwithdecimal"/>saadad </div> 
$(".allownumericwithdecimal").live("keypress keyup ", function (event) {     $(this).val($(this).val().replace(/[^0-9\.]/g, ''));     var text = $(this).val();     if (!((event.which >= 48 && event.which <= 57) || event.which == 8 || event.which == 46 || event.which == 110 || event.which == 0)) {         var text = $(this).val();         if ((text.indexof('.') != -1) && (text.substring(text.indexof('.'), text.indexof('.').length).length > 2)) {             //event.preventdefault();         }     }     var text = $(this).val();     if ((event.which >= 48 && event.which <= 57) && (text.indexof('.') != -1)) {         if ((text.substring(text.indexof('.'), text.indexof('.').length).length) > 2) {             //event.preventdefault();         }         if (event.which == 190) {             //event.preventdefault();         }     }     if (text.indexof('.') != -1 && event.which == 190) {         if (text.match("^[0-9]+(\.[0-9]{0,2})?$")) {} else {             $(this).val('');         }     }     if (text.indexof('.') == -1 && text.length > 7 && (event.which != 190 && event.which != 8 && event.which != 46 && event.which != 110 && event.which != 0)) {         event.preventdefault();     } }); 

http://jsfiddle.net/lx9h2smh/

the problem if type value in textbox 3434 , want make 35434 putting cursor after 3 , pressing 5, works fine in firefox , ie in chrome 5 added after value , becomes 34345.

the culprit line 1 replace non numeric characters.

how handle issue??

try code, runs. jsfiddle

i test

if ( /[^0-9\.]/g.test($(this).val()) ) {     $(this).val($(this).val().replace(/[^0-9\.]/g,''));  } 

explain

you make sure user enter value of want. replace if entered value not integer. regex mean: "those not integer or dot (.), replace them empty value". why need make test. therefore, if user enters value want, doesn't action replace , doesn't pass test.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -