There is such a piece of code: the result of if (RegExp.test(num)) is TRUE every time in IE, but in Fire Fox, if it is True the first time, it must be False the second time, and appears alternately thereafter. Let’s take a look at some past solutions: Method 1: Change if (RegExp.test(num)) to if (num.match(RegExp)) I don’t know if they have been verified. Anyway, I have tried and failed to achieve the compatibility effect. In fact, the most fundamental problem is not which regular object to use, but the "g" in the expression that causes the effect in Firefox to be inconsistent with IE. Firefox treats the RegExp as a global variable, so the simplest and most effective way is to change var RegExp=/^(-)?[0-9]*$/g to var RegExp=/^(-)?[0-9]*$/ , a good solution does not care about complexity, but the right solution. This situation is not common after all. function isNum(num){ var RegExp=/^(-)?[0-9]*$/g;
Method 2: Use RegExp object processing, that is, new RegExp("^(-)?[0-9]*$")
if(num.length==0){
return false;
}
if (RegExp.test(num)){
return true;
}else{
return false;
}
}