I need a regular expression to validate a string with the following conditions
- String might contain any of digits space + – () / .
If string contain anything else then it should be invalid - If there is any + in the string then it should be at the beginning and there should at most one + , otherwise it would be invalid, if there are more than one + then it is invalid
- String should be 7 to 20 character long
- It is not compulsory to have all these digits space + – () / .
But it is compulsory to contain at least 7 digit
I think you are validating phone numbers with E.164 format. Phone number can contain many other format. It can contain
.too. Multiple spaces in a number is not uncommon. So its better to format all the numbers to a common format and store that format in db. If that common format is wrong you can throw error.I validate those phone numbers like this.
Here I store
$e164in Db if its valid.Even after that you can not validate a phone number. A valid phone number format does not mean its a valid number. For this an sms or call is generated against the number and activation code is sent. Once the user inputs the code phone number is fully validated.