I have written a bunch of code to have a valid entry for Spend amount (32.4554, 234,324.34, etc.). I have additionally written code to limit the user from entering more than 13 digits (excluding . and ,).
All of this works perfectly in Firefox. But in IE 8, after the number of digits exceeds 13, if a letter is entered, Internet Explorer hangs and crashes! While debugging I noticed it faulting at the regex line, but the same regex works fine until that limit of 13 isn’t crossed! Regexperts, please! Show me the light!
function isIncorrectSpend(cubeCurrencySpend) {
if(!(/^([1-9]*[0-9]*,?[0-9]*)* (\.[0-9]*)?$/.test(cubeCurrencySpend))) {
return true;
}
return false;
}
function isLongerThanThirteenDigits(cubeCurrencySpend) {
cubeCurrencySpend = cubeCurrencySpend.replace(/,/g, "").replace(/\./g, "");
if(cubeCurrencySpend.length>12) {
return true;
}
return false;
}
jQuery("input#minval").keyup( function() {
if(isIncorrectSpend(jQuery("input#minval").val())) {
jQuery("input#minval").val("");
alert("please enter correct spend value");
}
});
jQuery("input#minval").keypress( function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(isLongerThanThirteenDigits(jQuery("input#minval").val()) && (code > 47 && code < 58)) {
alert("Please enter a number less than 13 digits");
return false;
}
return true;
});
I am quite sure this is a case of catastrophic backtracking. Your regex has too many possibilities to check, till it can fail.
The problem is this part
and I am quite sure it is not doing what you expect it to.
This will match the same stuff, but will fail much quicker:
it is still allowing stuff like 1,,,,,,,.123, but much better than yours. If you want to disallow such things, you need to clarify your requirements.