Phone number validation using JavaScript with .NET? - javascript

How do I validate phone numbers using JavaScript in ASP.NET?

Validating telephone numbers is a difficult problem.
Essentially, you define a regular expression that match the pattern(s) for a valid phone number. The conventions for a phone number are highly local-specific and without knowing something about the users' location, it's hard to generalize. My general rule is "Take what the user gives you, strip off everything but the digits and store only that, formatting it for display. Even that doesn't always work well, because somebody might give you a perfectly valid number like 555-1234 x345, because they can only be reached via a PBX extension. They've omitted the area code and if you strip off the non-digits, you're left with 5551234345 which would get formatted as 555-123-4335. Not so useful.
NANP (North American Numbering Plan) phone numbers have a 3-digit area code (optional), a 3-digit Central Office (CO/exchange) number and a 4-digit subscriber number, plus optional country code, access code, etc. Conventionally written as (variously, and omitting the country code and access code): (AAA) BBB-CCCC, AAA-BBB-CCCC, AAA.BBB.CCCC, etc. More formally, an NANP number (Zone 1, including the USA and its overseas territories, Candada and most Caribbean countries) should be written as +1:AAA-BBB-CCCC.
The French Numbering Plan currently has 10 digit telephone numbers, written as xx.xx.xx.xx.xx. It used to have 8-digit numbers, with Isle de France (metro Paris) having special rules. More formally, a French number should be written as +33:xx.xx.xx.xx.xx, except that if dialed form outside France, the leading '0' in the telephone number should be omitted, so from an outside perspective, the phone number should be expressed as +33:x.xx.xx.xx.xx.
Other countries and dialing/numbering plans have their own rules. Sometimes there are special rules in places regarding calls placed to and from specific locations in a given country (e.g., it used to be that calling a number located outside Isle de France from Paris required dialing a '16' prefix first.) Here in the US, some locations, require dialing all 10 digits even for local calls.
More info (and links) at the World Telephone Numbering Guide

function validatePhoneNumber(elementValue){
var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
return phoneNumberPattern.test(elementValue);
}
Use it like this:
if (validatePhoneNumber('123-456-7890'))
{
// do something
}

Related

How to get the decimal separator (period or comma) displayed in the input at the moment?

As far as i see, there is no way to replace the decimal separator that the browser displays for non-integers(if you want them to always be a period or always a comma, regardless of the localization). These are the actions that the browser performs under the hood taking into account the browser language and localization. Details can be found here.
Localization of input type number
https://www.ctrl.blog/entry/html5-input-number-localization.html
Therefore, I had a need to at least consider what is currently displaying an input, period or comma, and depending on this, change the contents of the label.
So, basically what i need is function or something else, that will tell me what decimal separator is displaying now.
return comma if it's comma
return period if it's period

Javascript regex- phone number

i'm trying to find an expression that matches a phone number in the format of (0[2,3,6,7,8,or 9]) XXXXXXXX where X is a digit, the space must be matched and so must the parentheses
My current expression is:
/\b\(0[236789]\)\s(\d){8}\b/g
but it's not picking up any test numbers such as
(02) 12345678
I know regex phone number questions get spammed on SO. I have been reading through all the ones I can find which is how I've made it to this point but I can't for the life of me figure this out.
Just remove the \b on either end, and it should work
/\(0[236789]\)\s(\d){8}/g
This will match multiple phone numbers, not sure if that is what you want. If you want to make sure the entire string from start to finish is the full phone number, you can do this
/^\(0[236789]\)\s(\d){8}$/
This will match (02) 12345678, and won't work if there are any characters around the string.
Well, this is the best I can come up with.
/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|\d{2}\-|\d{2}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})
(\-|\s)?(\d{4})$/g
This should support all combinations like
(541) 754-3010
+1-541-754-3010
1-541-754-3010
001-541-754-3010
191 541 754 3010
+91 541 754 3010
and so on.

Transform a specific secret 6 digit number into another 6 digit number

Is there a way to have a specific 6 digit number to transform into another 6 digit number?
It's meant to be used into a User Interface where the user must know a secret number and it will result into another number.
For instance, the number to obtain is 123456 and the number the user should input is 111111.
So if the user inputs 111111, he will see 123456 which is the right number.
If the user input 222222, he will see something else than 123456 which won't be the right number.
The goal is to avoid telling the user weither he knows the right or wrong secret number to begin with. He could try every combination but won't know 111111 was the right one.
Usually you might take something like a password field that completely hides the input. This way neither your user nor any spectators can guess the right input.
Ok, i solved it.
Plain simple...
I just need to have an encryption that add 0 to the first digit, 1 to the second one, etc.
1111111 will become 123456 (right number), 222222 will become 234567 (useless number).
And nobody can figure the key is 111111.

Reg Expression Javascript for Millions with limit

I am looking to create a regular expression in javascript that does the following:
Allows for 1 or more numbers
Then has an optional period (".")
Then has an optional number of digits up to 6
The context is that i need people to enter in numeric values in the millions and i want them to at least include a 0 if they are entering thousands... so they could enter the following:
1 (would be one million)
0.725 (would be 725k)
10.5 (would be 10M 500K)
I also need to ensure that the value doesn't reach over 725.00 (or 725 million).
Thanks in advance.
That sounds like:
/^(?!\d{4})(?![89]\d\d)(?!7[3-9]\d)(?!72[6-9])(?!725\.0*[1-9])(0|[1-9]\d*)(\.\d{1,6})$/
which means:
doesn't start with four digits (i.e., is less than 1000)
doesn't start with 8 or 9 followed by two digits (i.e., is less than 800)
doesn't start with 73-79 followed by a digit (i.e., is less than 730)
doesn't start with 726-729 (i.e., is less than 726)
doesn't start with 725. followed by zero or more zeroes followed by a nonzero digit (i.e., is less than or equal to 725.00).
starts either with 0, or with 1-9 followed by zero or more digits
after that, optionally a decimal point followed by between one and six digits
That said, I'd actually recommend implementing the above as several separate checks, rather than cramming it all into one regex like the above. In particular, the "is less than or equal to 725.00" check is probably better implemented using numeric comparison; and even if you do want to use a regex for that, you probably want to detect it as a separate error from 0.1asefawe so you can give a more precise error-message.
So basically you want a number that would be multiplied by 10^6 to get the true value.
This sounds like a two-stepper; First, verify that the input string is in a format you expect (you can use a regex for this very easily). Then, parse the string into a number variable and test the actual value. The regex pattern for that would look like "[0-9]{1,3}(\.[0-9]{1,6})?", basically matching a number with up to 3 whole digits and 6 fractional digits, the decimal place and fractional digits being optional. If it matches this pattern, then it's parsable into a number, and you can then perform a quick check that your number <= 725.
I honestly don't think it's feasible to create a single Regex that can validate a proper numeric format AND an inclusive maximum range, but here's a start:
"^(725(\.0{1,6})|(([7][2][0-4]|[7][0-1][0-9]|[1-6][0-9]{2}|[1-9][0-9]|[0-9])(\.[0-9]{1,6})?)$"
This will allow any natural whole number from zero to 724, with any fractional part up to six digits from ".000001" to ".999999". It does this in stages; it will match 720-724, or 700-719, or any three-digit number up to 699, or any two-digit number, or any one-digit number. Then, it will also match the quantity "725" explicitly, with an optional decimal point and up to 6 zeroes.
EDIT: While your comment states that you used this pattern, and it does produce the correct result, I had intended it as a "what not to do"; this pattern will be far more costly to evaluate than the first solution, just to avoid a server-side rule check. And you will have to perform a server-side validation anyway; anything done within the confines of the user's browser should be suspect because the user can disable JavaScript or can even use browser plug-ins like FireBug to make your HTML page behave the way he wants, instead of the way you designed it.

Getting the first dollar amount using JavaScript with Razor and .Net

I asked a similar question on how to do this on the server side (SQL), however it makes more sense to accomplish this on the client side, based on the app architecture.
I've got a MVC3 app with Razor on the .Net framework, where I have model data available that I would like to parse and return the first dollar value from a given string using Javascript / regex,
For example, each of the following lines represents a sample data set:
Used knife set for sale $200.00 or best offer.
$4,500 Persian rug for sale.
Today only, $100 rebate.
Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf.
I've seen a few issues already including the # in JS and a few other pitfalls I would like to try to avoid.
Thanks.
var m = line.match(/\$[0-9,]+\.?\d*/);
if (m)
return m[0];
should give you a hint. This Regex returns you a string which consists of a dollar sign, some numbers or commata, and optional a dot another few numbers behind it. You might want to limit its wideness (only 2 decimals, not starting with zero etc).

Categories