Phone number validation

Ask about a PHP problem here.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Phone number validation

Post 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...
  1. - 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Phone number validation

Post 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
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Phone number validation

Post by Helx »

Try: substr($contact_phone, -9, -7)

In the PHP manual it uses an example sorta like that.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post by EcazS »

Not sure if this one works or not, but you can try it.
http://stackoverflow.com/questions/8343 ... -functions
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post 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.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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}
}
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Phone number validation

Post 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
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post 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.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post by Fidbeck »

like this right?
if(startsWith(91)) {
    true
}else {
    if(startsWith(92)) {
        true
    }else {
    if(startsWith(93)) {
        true
}else {
    if(startsWith(96)) {
        true
}
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post by EcazS »

No, use elseif. Read the link I posted...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Phone number validation

Post 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.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post by EcazS »

But do they start with 91, 92, 93 or 96? ;)
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post by EcazS »

You can do like this,
<input type="text" name="field1" value="<?php if(isset($_POST["field1"])) { echo $_POST["field1"]; } ?>" />
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post by Fidbeck »

Thanks.

and what about the phone problem?
Thanks
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post 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.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Phone number validation

Post by jacek »

if (!preg_match('#^9[1236]{1}[0-9]{7}$#', $number)){
It just about getting the right expression :)
Image
Post Reply