Home >Web Front-end >JS Tutorial >Issues you need to pay attention to when using the parseInt function in Javascript_javascript tips

Issues you need to pay attention to when using the parseInt function in Javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:06:231128browse

A few days ago, the credit card site was going to integrate a new feature, but it was really tricky. Asp site, everyone knows that there is an expiration date on the back of the credit card. When connecting to the bank, this information must be sent to the bank for data processing. Verification, after the user inputs the validity period of the credit card by voice, the system will make a validity period judgment. In order to avoid unnecessary trouble, it is judged that the expiration time must not be within one month. Since the input year, month and day are in three text boxes, plus I thought it was troublesome to convert it into time, so I simply converted the text content of year, month and day into int type to judge. This is the background.

Having said so much, I finally got to the topic of the article. We know that the validity period is like this: 2015-09-20. I took out "09" from the month text box without hesitation, and then pooped. I made the following conversion under chrome. I'm very happy and it's done.

In the afternoon, the little sister who tested it came back and said that there was a problem in judging the validity period of the credit card. I was quite surprised. No matter how I tried it on my own machine, there was no problem. Then I had no choice. I could only go to the little sister’s desk to see it in person, apply some oil by the way, and start it. After debugging with the IE debugger, sure enough, a supernatural event occurred, and now I finally found the problem.

When I was testing, my little sister installed IE8, and under IE8, parseint would treat my "09" as octal by default, but found that it was not a legal octal, and finally threw out the false value of 0. We also saw that in Under Chrome, "09" will be viewed as decimal by default, so the js engine will naturally convert it into 9, which I am satisfied with. So, IE really does not follow human common sense. Now we should solve it Woolen cloth?

1: Solution

<1> I also discovered in the discussion just now that parseInt has different default behavior processing in different browsers. I have no idea. I can’t see how the underlying processing of parseint is handled. I can only look for the javascript api document. I went to take a look at the black box, and then I found a radix parameter that surprised me.

Knowing this radix, the problem can be easily solved. Now I only need to specify radix=10, as shown in the picture:

However, it was later discovered that under IE9, the bottom layer of parseint also defaults to decimal when radix is ​​missing. Finally, Microsoft made the correct modification to meet the public's taste.

<2> In addition to the parseint function, there is actually a Number function that can also help you solve this problem, and there is no problem on IE7 and 8, because in js, if you go to new and look at this Number If so, you can generate a Number object. This is somewhat different from C#, so I can look at it in two ways below.

The advantage of this Number is that it can only handle decimal values, and there are other special ways to play it. Unlike parseint which only accepts string parameters, number can accept any type, such as:

Since we cannot see the underlying source code of Number, we can only remember these issues through hearsay. This is where JS is different from other managed languages.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn