javascript regular expressions phone number - phone-number

I have written a regular expression for phone number for different countries. But, I din't resolve problem: when user enters phone number +345)-234355464 (with one bracket for country code) this phone number is valid. This is incorrect behavior. It should be two brackets or none. Please, advise me how to resolve this problem? below is my regular expression.
/^(\+?((\(?\d{2}\)?[-.\s]?(\d{4}))|(\(?\d{3}\)?[-.\s]?)))([0-9\s-]{1,14})$/

Related

(wx)Maxima: determining the number of parts of an expression

I'd like to use part to handle expressions of different length but have not been able to find anything in the documentation that addresses how to determine the number of parts of an expression.
I do have an upper bound for the number of parts so, in this particular case, I could loop over the terms until I get an error; however, I was wondering if there is a more direct method?
I can't believe this, but it appears length does the trick.
I assumed it was limited to lists because it returns an error for single numbers, because they are atoms. Apparently my brain decided "doesn't work with atoms" to mean "only work with lists."
However, this does mean that neither part nor length will work if the expression only has one part, so that case has to be handled separately.
I would still be interested in knowing if there is a solution that will work in all cases, so I won't mark this as the answer, just yet.

Match Nothing or pattern in regular expressions

I am trying to validate fields in my iOS program:
I need to match a phone number, but the field is optional.
I thought using the regex to match the number to also validate if there is no phone number:
[0-9\-\+\*]{4,14}
Then I thought how to also match where there is either a valid number or no number at all?
(:?[0-9\-\+\*]{4,14})?
Meaning, either match between 4 to 14 chars within the range 0-9,+,-,* or nothing.
This website is showing infinte matches for that pattern.
ideas?
^$|^[0-9\-\+\*]{4,14}$
As to the questions this has brought here:
Regex is a great validation method. and it is cross platform.
no need for another layer of code to implement. Simple and clean.
You should just code it. I don't know your language but basicaly :
If(field.isEmpty)
should do the trick.

phone number format mask in ios

I'd like a regex or some function to standardize all phone number input from my ios app.
Thus inputs like the following
(212)555-5555
2125555555
212-555-5555
212 555 5555
212-5555555
all get translated to
(212) 555-5555
would this following regex work to match the phone numbers into 3 match groups which I can then format into the correct output string?
^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$
Is the $ sign at the end of the regex required or does that mess things up?
Is regex the best way to do this in iOS?
^ and $ in a regex match the beginning and end of a line of input. You have not provided enough information to determine if these anchors are appropriate in this particular case.
Since you're working in iOS have you looked at the NSDataDetector class? It provides mechanisms for detecting strings which could be valid phone numbers in many different formats. This would give you phone number detection matching the behavior users see in many of the other apps on their devices.
NSDataDetector does not provide a mechanism for re-formatting phone numbers so you would still need to determine how you want to reformat strings detected as possible phone numbers (which may contain more or less than 10 digits). If you do so you should probably fall back to preserving the original format of any detected number which does not match one of your expected formats.

Postal Code verification in Scheme (Dr. Racket)

I am writing a program in Scheme (Dr. Racket) to verify Canadian postal-codes. The user inputs a postal code and gets a response whether it is valid or not. I got the boolean logic down but I am stumped as to how to actually tell it what the correct format is.
ex. (valid-postal-code? N2L 3G1) => true
How do I do this?
Thanks
If you want to know if a string has the format of a valid postal code, you can use a regular expression. Canadian postal codes consist of six characters, alternating letters and digits beginning with a letter, with a space embedded between the third and fourth characters. A suitable regular expression is ^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$.
if you want to know if a string with a valid format is on the list of postal codes, the easiest solution is a bloom filter. I provide a bloom filter, written in Scheme, at my blog.
I don't know how Canadian postal codes work, but I think what you are asking is that you probably just have a long list of valid codes and need to tell the program that they are ok, and no other codes are.
Using a mutable hash map would be ideal for your purpose: http://docs.racket-lang.org/guide/hash-tables.html

Other causes for EConvertError with StrToFloat() in Delphi 6 application?

I'm having a strange problem that is affecting at least some of my international users of my Delphi 6 application. Here's the scenario:
My program requests status reports periodically from an external device that acts as an HTTP server.
The device sends back the status report as a response document that has a series fields delimited with the pipe character in name value pair format (e.g. - field1=-0.437).
I split the report string into the fields and then again to get each field name and numeric value.
I use StrToFloat() to convert the floating point field values in string format and assign the result of that function to a Variant variable.
This works fine on most PCs, but some of my international users are getting EConvertError's when I try to use StrToFloat() on the numeric values. Here's a concrete example of an error message from my logs:
EConvertError: '-0.685' is not a valid floating point value
As you can see -0.685 is a valid floating point number, yet I am getting the EConvertError Exception. Normally I would expect to see a comma where the decimal point is, or some other locale specific punctuation problem, but the number appears fine in this case. Also, to the best of my knowledge the external device does not even have the option to set the character set.
So what subtle nuance about Delphi 6 and international character sets might be causing this problem, perhaps related to the user's Windows XP/Win7 character settings? Note, I use standard Delphi 6 "string" cast strings throughout my program so I don't see how a multi-byte character set issue could be the root cause. Has anyone had this problem and knows what to do about it?
Your remote user's machine is expecting , for the decimal separator. When it encounters . the EConvertError exception is raised. On a machine which expects , as the decimal separator (e.g. most European and South American countries) -0.685 is indeed not a valid floating point value.
Normally I would expect to see a comma where the decimal point is, or some other locale specific punctuation problem, but the number appears fine in this case.
Your current problem is just the flip-side of the above issue. Normally, because your locale uses . as the separator, you are accustomed to seeing problems when data with , is used instead. Put yourselves in the position of somebody from a country which uses , as a separator. For them, they will be accustomed to seeing exceptions when data with . is used.
You could solve the problem by normalising the input to use the same decimal separator as the the machine locale. On a modern Delphi you could solve the problem by use the StrToFloat overload that receives a TFormatSettings parameter and explicitly specify that . is to be used as the decimal separator for this conversion. Unfortunately that facility is not available in Delphi 6.
I faced this issue for Belgian users. I also had to manually replace the '.' or ',' in the input data.
Also, if you are inserting the data into the database (sql) then, you will have to replace the ',' with '.' during insertion of the data into the database.

Resources