I have a form and would like to see if the user filled the input with a correct number or not. The number must contains 10 digits and the two first digits should be equal to 05
or 06
. Can anyone help me how to do this in PHP or in JavaScript please?
Asked
Active
Viewed 164 times
-2

SparkFountain
- 2,110
- 15
- 35

love coding
- 59
- 1
- 1
- 4
-
[Here](https://stackoverflow.com/questions/47284533/how-to-set-validation-on-phone-number-in-php) your answer – Simone Rossaini Jan 13 '20 at 14:44
-
1Does this answer your question? [Phone number validation Android](https://stackoverflow.com/questions/6358380/phone-number-validation-android) – B--rian Jan 13 '20 at 14:45
-
welcome to stackoverflow, You can use regex and javascript to reach those kind of validation or u can find a plugin or a validation lib for that can works depends... – Aziz.G Jan 13 '20 at 14:45
-
1Hey there. On SO, you're expected to show the code you have, then detail what your expected output/result is vs. what you currently get, plus all error messages, if any. Currently, it looks like you're just asking us to write code for you, which will get your question closed pretty soon. I suggest to check out [how to ask a good question](https://stackoverflow.com/help/how-to-ask), then edit yours accordingly. Cheers! – domsson Jan 13 '20 at 14:47
-
1Does this answer your question? [How to set validation on Phone number in PHP](https://stackoverflow.com/questions/47284533/how-to-set-validation-on-phone-number-in-php) – Altherius Jan 13 '20 at 15:40
2 Answers
0
According to comments and example phone number that you given, this code will validating number of digits and first two numbers of your country, i just replaced + with 0, for sure they won't enter plus.
$tel = '0512345678';
if(preg_match("/^0[5|6][0-9]{8}$/", $tel)) {
echo "valid";
} else {
echo "invalid";
}

Mufaddal Hamid
- 281
- 3
- 12
0
Would you like to validate the phone number on the server or in the client (user's web browser)? In the first case you should use PHP, in the latter case a JavaScript function.
PHP:
function isValidPhoneNumber($number) {
return preg_match('/^0[5|6][0-9]{8}$/', $number);
}
JavaScript:
function isValidPhoneNumber(number) {
return /^0[5|6][0-9]{8}$/.test(number);
}

SparkFountain
- 2,110
- 15
- 35