Javascript code for validating numbers


It’s quite another to make a form completely unusable, for example by forcing people to provide numbers in a certain country’s format.Nevertheless, they can be effective if you know that certain numbers will be within a particular range.exclusive deals on our favorite books, courses and tools outside of Site Point, you won't find anywhere else." image="" image_alt="Site Point Resources" button_class="u-large" button_text="Get This Deal Now" button_link="/premium/l/join?ref_source=sitepoint&ref_medium=article-pushdown" bg_color="#262626" bg_pattern="" ga_event_label="maestro-670" / When you’re collecting data from users, there are two key challenges; collecting that information, and validating it. In this article I’ll discuss some of the challenges around collecting, validating, and displaying telephone numbers.Numbers must also be altered according to where you’re dialing from.From abroad, to call a UK number you need to drop the leading zero and prefix with the dialing code 44: Thankfully, there is a format we can use which enable us to get around these variations.In some cases, the context might mean they aren’t required.For example if you operate in a single country, and telephone numbers are captured to be used by a human operator, you might not need them.

javascript code for validating numbers-32javascript code for validating numbers-49javascript code for validating numbers-15javascript code for validating numbers-32

Should we decide that a regular expression is sufficient – and remember, there are issues – then we can use the Masked inputs are a common technique for restricting user input or providing hints as to the expected format.

Luckily for developers there is an unambiguous, internationally recognized standard for telephone numbers anywhere in the World called E.164.

The format is broken down as follows: We can represent any valid telephone number using the E.164 format.

Some types of information are straightforward – someone’s age, for example, couldn’t really be simpler to collect and to validate. Perhaps you’re thinking that since telephone numbers tend to follow a pretty rigid format, such as this: So based on that, we know that the regular expression apparoach isn’t as simple as we first thought – but that’s only the half of it. Sure, if you know that the number you’re collecting is going to be for a specific country, you may be able to use a regular expression. Let’s look at some of the other issues around telephone numbers, and why they make our job even harder.

Names aren’t as straightforward as they sound, but provided you cater for edge cases and international variations – for example patronymics, the mononymous, or even just people with hyphenated surnames – you can’t go too far wrong (although plenty of applications and services do! Email addresses, while theoretically very easy to validate, have their own challenges – yet nevertheless, there are plenty of regular expressions in the wild that aren’t quite right. All sorts of external factors can have implications for telephone numbering.

Even then, the capital had a subtly different system.

You must have an account to comment. Please register or login here!