I know this should be easy.... However, to validate a 7-digit phone number, I wrote this:
<?php
if(!empty($_POST['mobile_number'])) {
if(!is_numeric($_POST['mobile_number']) || strlen($_POST['mobile_number']) != 7) {
$errors[] = 'Please enter a 7-digit number without spaces or dashes for your <strong>Mobile</strong>.';
} else {
$_SESSION['mobile_number'] = $_POST['mobile_number'];
}
} else {
$_SESSION['mobile_number'] = '';
}
... but now I realise that numeric/integer validation won't work because where I am - in Ireland - mobile numbers can start with an initial zero, too. So, the following mobile numbers are possible:
0123456
0224466
How can I ensure that:
[*]each digit in the mobile number is numeric
[*]an initial zero is possible
[*]there are exactly 7 digits
I suppose I loop through each digit, ensuring that that digit is numeric and break out of the loop if a non-numeric digit is encountered..?
Any suggestions?
TIA