Issues to be aware of using parseint functions in JavaScript _javascript tips

Source: Internet
Author: User

A few days ago, the credit card site was connected to a new feature but really compared to pit Dad, ASP site, we all know that the back of the credit card has a valid, in the docking bank this information must be passed to the bank to do data verification, the user in the voice input credit card validity, the system will make a valid judgement, For unnecessary trouble, is to determine the expiration time must not be within one months, due to the input of the year in three text boxes, plus I think turn into a time of trouble, simply directly with the years, months, days of the text directly to the content of the type of int to judge, this is the background.

Said so much, finally said the article topic, we know that the validity period has such: 2015-09-20, I did not hesitate to take out the "09" from the Month text box, and then fart in Chrome under the following conversion, very happy, buttoned.

The afternoon test little sister feedback to say that credit card validity of the judge has a problem, more surprised, in their own machine how to try all no problem, and then no, can only go to the little sister table to see, by the way of oil, open the IE debugger debugging, sure enough, the supernatural incident, now finally found the problem.

At that time, my little sister was pretending to be IE8, then under the IE8 parseint default will be my "09" as octal, but also found that is not a legitimate octal, and finally threw 0 this false, we also see that under the Chrome, "09" will default as a decimal point of view, So the JS engine will naturally be converted to 9 of this I am satisfied with the results, so, ie really is not according to the common sense of people out of cards, now we should solve it?

One: the solution

<1> also found in the discussion, parseint in different browsers have different default behavior processing, and can not see how the parseint bottom is handled, only to find JavaScript API documents this black box to see, And then I find a radix parameter that surprises me.

Knowing this radix, the problem can be solved, now I just specify the next radix=10 can be, as shown:

But later found in the IE9 below, parseint in the absence of radix when the bottom is also the default by the decimal, and finally Microsoft to meet the public taste to do the correct changes.

<2> In addition to parseint this function, there is actually a number function can also help you solve this problem, and ie7,8 above is no problem, because in JS, if you go to new this number, it can generate number object , there are some differences between this and C #, so I can look at it in two different ways.

The advantage of this number is that it only handles decimal values, and there are other special rules, unlike parseint, which accepts only string arguments, number can accept any type, such as:

Because I can't see the source code at the bottom of the number, so for these problems, we can only hearsay to remember, this is JS and other managed language is not the same place.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.