Page 1 of 2
Phone number validation
Posted: Wed Sep 26, 2012 12:01 am
by Fidbeck
Hi
In my contact form I've added a field where the user can send his/her phone number.
What I want to do is...
- - Since the field is not mandatory it should check if it is empty or not. if empty will send it empty
- If not empty it should check if the number start with 91, 92, 93 or 96 and it has to have 9 digits total.
Here's my code
if (empty($contact_phone) === true) {
// send nothing...
} else {
//if (substr($contact_phone, 0, 2) != '91' || substr($contact_phone, 0, 2) != '92' || substr($contact_phone, 0, 2) != '93' || substr($contact_phone, 0, 2) != '96') {
if(($contact_phone >= 0 && $contact_phone < 910000000 || $contact_phone >= 940000000 && $contact_phone < 960000000) || ($contact_phone >= 970000000)){
$errors[] = 'Phone numbers needs to start with 91, 92, 93 or 96. Your number: ';
}
}
I tried those two ways but I want to know if there is another and easier way to do it.
Re: Phone number validation
Posted: Wed Sep 26, 2012 4:59 am
by Helx
I won't post how I'd do it, because many people type in their phone number differently.
You may have your substr wrong (for what your trying to do, I think you need to use negative numbers)
http://nz.php.net/manual/en/function.substr.php
Re: Phone number validation
Posted: Wed Sep 26, 2012 8:26 am
by Fidbeck
Negative numbers? I'm not getting it sorry.
With the substr when I tried all numbers were accepted even those who don't even start with 91, 92, 93, 96.
the format I want is this 91XXXXXXX for example.
The other part where are the conditions, it seems to be working, but I want to know if there is a easier way, instead of using conditions.
Re: Phone number validation
Posted: Wed Sep 26, 2012 8:41 am
by Helx
Try: substr($contact_phone, -9, -7)
In the PHP manual it uses an example sorta like that.
Re: Phone number validation
Posted: Wed Sep 26, 2012 8:46 am
by EcazS
Not sure if this one works or not, but you can try it.
http://stackoverflow.com/questions/8343 ... -functions
Re: Phone number validation
Posted: Wed Sep 26, 2012 9:21 am
by Fidbeck
abcedea
I've replaced the 7 you had for 1, 2, 3 and 6.
gonna try it
EcazS
doesn't seem a very good way to do it.
If you mean the part begins with and ends with because that would be starts with 9 and could end with another 9. The first is 9 and the last could go from 0 to 9
By the way this is the link for the contact
http://exp3.comyr.com/contacti.php
EDIT1:
abcedea doesn't seem to be working even if I put a valid phone number like 910000000
Re: Phone number validation
Posted: Wed Sep 26, 2012 9:32 am
by EcazS
You could just do
if(startsWith(91)) {
true
}elseif(startsWith(92)) {
true
}
Not pretty but if the function works I don't see why not.
Re: Phone number validation
Posted: Wed Sep 26, 2012 9:36 am
by Fidbeck
I'm not very good at this because i'm starting to learn it but doesnt seem to be missing something in you code?
Shouldn't be
if(startsWith(91)) {
true
}else {if(startsWith(92)) {
true}
}
Re: Phone number validation
Posted: Wed Sep 26, 2012 9:37 am
by Helx
Still need the actual function don't you?
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
Oh, and you made it look like I edited the post :3
SOURCE: http://stackoverflow.com/questions/8343 ... -functions
Re: Phone number validation
Posted: Wed Sep 26, 2012 9:42 am
by Fidbeck
I'm kinda sleepy today and i'm not entirely understand what you are saying but I'm starting to get and idea of what you'r saying.
to check the length of the $contact_phone field I already have this
if(!empty($contact_phone)){
if (strlen($contact_phone) > 9 || is_numeric($contact_phone) === FALSE) {
$errors[] = 'Phone number needs to be below 10 characters, using only number with no spaces.';
}
}
I found a function called REGEX or something like that but I don't understand how that works entirely.
Re: Phone number validation
Posted: Wed Sep 26, 2012 10:00 am
by EcazS
abcedea wrote:Still need the actual function don't you?
Read the post I made before?...
It could be like this,
if(startsWith(91)) {
true
}else {
if(startsWith(92)) {
true
}
}
but you can continue on with elseif and have a "default" else later, read up on this link
http://php.net/manual/en/control-structures.elseif.php
Furthermore, the function I linked is if you DO NOT want to use regex. Since you don't understand regex I advise you to just use the function I linked.
Re: Phone number validation
Posted: Thu Sep 27, 2012 4:48 pm
by Fidbeck
like this right?
if(startsWith(91)) {
true
}else {
if(startsWith(92)) {
true
}else {
if(startsWith(93)) {
true
}else {
if(startsWith(96)) {
true
}
Re: Phone number validation
Posted: Thu Sep 27, 2012 7:05 pm
by EcazS
No, use elseif. Read the link I posted...
Re: Phone number validation
Posted: Fri Sep 28, 2012 1:10 pm
by jacek
Why not just do it the
correct way and use a regular expression
if (!empty($contact_phone) && !preg_match('#^[0-9]{6,9}$#', $contact_phone)){
// errors
}
This would have to be numbers only between 6 and 9 characters long.
Re: Phone number validation
Posted: Fri Sep 28, 2012 1:17 pm
by EcazS
But do they start with 91, 92, 93 or 96?
Re: Phone number validation
Posted: Fri Sep 28, 2012 10:08 pm
by Fidbeck
Thanks for all your replies guys. really appreaciated.
Jacek i'm sorry but it seems that you missunderstood what I wrote.
the phone number is 9 chracters long
the first is 9 and the second has to be 1 or 2 or 3 or 6
like in this
91xxxxxxx
92xxxxxxx
93xxxxxxx
96xxxxxxx
EDIT1: I already knew of this but isn't there another way to do it?
to make the fields "save" what we had written in case of errors
value="<? echo $contact_name ?>
this is the way but even if you send it and no errors are found the name still remains.
Re: Phone number validation
Posted: Mon Oct 01, 2012 8:00 am
by EcazS
You can do like this,
<input type="text" name="field1" value="<?php if(isset($_POST["field1"])) { echo $_POST["field1"]; } ?>" />
Re: Phone number validation
Posted: Mon Oct 01, 2012 9:30 am
by Fidbeck
Thanks.
and what about the phone problem?
Thanks
Re: Phone number validation
Posted: Mon Oct 01, 2012 12:33 pm
by EcazS
Sorry about the mess and confusion, but for some reason the function I linked didn't work for me.
So here is how you can do it instead, I made an example but hopefully you can pick out what's going on.
if(isset($_POST["submit"])) {
$phoneNumber = $_POST["phone_number"];
if(!preg_match("#^[0-9]{9}$#", $phoneNumber)) {
$errors[] = "Phone number must be 9 numbers long.";
}
if(!preg_match("/^91|92|93|96/", $phoneNumber)) {
$errors[] = "Number does not start with 91, 92, 93 or 96";
}
if(empty($errors)) {
//success!
//do something else
}
}
So first I just check if they have submitted the form and then I set the post variable to the phoneNumber variable.
The first preg_match checks if the number is NOT 9 numbers long and it only allows numbers. So if someone types in "12345" it will return an error because it's only 5 numbers long and if they type in "abcde" it will return the same error because it's not numbers, it's letters.
And the second preg_match checks if the phoneNumber variable DOES NOT start with "91" OR "92" OR "93" OR "96". So it will return an error if it doesn't start with either of those.
So you could just pick out the two if(preg_match) statements and put them in your code. May have to change from $phoneNumber to something else though.
Re: Phone number validation
Posted: Mon Oct 01, 2012 9:40 pm
by Fidbeck
sent you a PM.
thanks
I tried the code but it doesn't seem to be working because it allows to send number like 950000000
Re: Phone number validation
Posted: Wed Oct 03, 2012 3:15 pm
by jacek
if (!preg_match('#^9[1236]{1}[0-9]{7}$#', $number)){
It just about getting the right expression