This article mainly introduces the issues that need to be paid attention to when using the parseInt function in Javascript. This article explains how to solve the compatibility problem that the parseInt function may return 0 values in IE8, if you need a new function, you can refer to the credit card website to access a new function the next day. However, it is really a pitfall, asp site. everyone knows that there is a validity period on the back of the credit card, in bank connection, this information must be sent to the bank for data verification. after the user enters the credit card validity period, the system will make a validity period judgment. for unnecessary trouble, that is to say, the expiration time must not be within one month. because the input year, month, and day are in three text boxes, and I think it is time-consuming, I simply get the year, month, the text content of the day is directly converted to the int type. this is the background.
After talking so much about the topic of the article, we know that the validity period is as follows:, I did not hesitate to extract "09" from the monthly text box ", then I made the following conversions in chrome, so I am very happy to do it.
In the afternoon, the test sister reported that there was a problem with the credit card validity period judgment, which was quite surprising. there was no problem with how to try it on her own machine. then, I had to go to my sister's desk and check it out. By the way, I just need some oil, open the IE debugger and debug it. as a result, a strange event occurred, and now the problem is finally found.
At that time, the test sister installed IE8, and then in IE8, parseint will regard my "09" as the "gossip" by default, but found that it is not a legal gossip, and finally throw the "0" false, we can also see that in chrome, "09" will be regarded as decimal by default, so the js engine will naturally convert to 9, which is my satisfaction. so, IE is really not playing the cards according to common sense. Should we solve it now?
I. Solution
<1> We also found in the discussion just now that parseInt has different default behavior processing in different browsers, and we can't see how parseint is processed at the underlying layer, you can only go to the black box of the javascript api documentation and see a radix parameter that surprised me.
Now that you know this radix, you can solve the problem. now I only need to specify radix = 10,
However, it was later published below IE9. when parseint lacks radix, the underlying layer uses decimal by default. Finally, Microsoft made correct changes to suit the public taste.
<2> In addition to the parseint function, there is also a Number function that can help you solve this problem, and there is no problem in IE7, 8, because in js, if you try to create a new Number, you can generate a Number object. this is different from C #. so let's take a look at it in two ways.
The advantage of this Number is that it only processes decimal values, and there are other special methods. Unlike parseint, it only accepts string parameters, and number can accept any type, for example:
Because we can't see the underlying source code of Number, we can only hear it to remember these problems. this is also a difference between JS and other hosting languages.