Page 1 of 1

Form Validation For Phone Number

Posted: Fri Dec 23, 2005 4:47 pm
by dwessell
Sami | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hey all.

I'm attempting to validate a grouping of phone numbers.

One the page, I have a for loop that generates a grouping of phone number forms, based on user input.

The end result are phone number variables that look like:

$phoneNumber0
$phoneNumber1
$phoneNumber2
etc..

I'm wanting to check those variable for proper phone number input (In my case, 10 digits only "1231231234").

What I've been trying to do is:

Code: Select all

for($i=0;$i <= $numberTeams; $i++){
    	$tmp = preg_match (' /^[0-9]+$/ ', " $phoneNumbers'.$i.' " );
    	if ($tmp != 10){
        echo "$tmp";
    	echo "I'm sorry, but one of the phone numbers that you entered is invalid";
    	echo '<br>';
    	echo "Please click the back button, and try again";
    	exit;
    	}
    }
But I can't seem to get an understanding of preg_match. I seem to get a result of 1, not matter what. My understanding would that the above, would check for anything in the range of 0-9, and return the number of matches? But I seem to get a 1 back, no matter what I put in.. Even chars.

Can anyone offer advice?

Thanks
David


Sami | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Dec 23, 2005 6:43 pm
by supernerd
Check out zoopframework.
It has built in type validation including phone type. It handles this using guiControls, which once you try you will wonder how you even programmed forms without them.

It has integrated javascript and php validation and stores the users input as well, so they don't have to press back or lose any of their inputed data.

you can find it at http://zoopframework.com. There is a forum there as well if you need help with it.

Posted: Fri Dec 23, 2005 6:56 pm
by dwessell
Hey supernerd,

Thanks for the tip.. I will check them out..

I still want to figure this out, as I'm learning php (I already code in c++) and need to learn the syntax differences.

Thanks
David

Posted: Fri Dec 23, 2005 10:20 pm
by josh
This is more of a regex problem, but preg_match returns a boolean of wether or not the string matched
as documented at http://www.php.net/preg_match

try a regex like:

Code: Select all

^([0-9]{10})$

for help on regex check out d11's tutorial, it's stickied in the regex forum on these boards



edit: okay it does return the # of matches but it's effectively a boolean

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match.

so it's either 0, no match.. or 1, a match.

Posted: Sat Dec 24, 2005 6:01 am
by dwessell
Hey jshpro2.. Thanks for the information...

I really appreciate that response, it's a learning experience for me.

What I'm working with now, althought I still haven't gotten it to work is:

Code: Select all

ereg('/^([0-9]{10})$/',"$phoneNumbers'.$i.'",$regs);
	$tmp = count($regs);
	echo "$tmp";
	if($tmp!=10){
Any other tips are always appreciated. I think the issue may be "$phoneNumbers'.$i.' " but so far I haven't been able to prove it yet...

Thanks
David

Posted: Sat Dec 24, 2005 9:52 am
by josh
You probably want

Code: Select all

$phoneNumbers[$i]
with no quotes around it at all


also instead of storing $regs as the match and counting it, use the return value of hte function

Code: Select all

$found =preg_match(......);
if (!$found) {
// not a valid phone number
}

Posted: Sat Dec 24, 2005 12:51 pm
by RobertGonzalez
dwessell, trying validating the content of the string first, then checking its length to make sure it is ten characters long.

Code: Select all

for($i = 0; $i <= $numberTeams; $i++) {
    $checkNumber = $phoneNumbers'.$i;
    if (preg_match ('/^[0-9]+$/', $checkNumber) {
        if (strlen($checkNumber) = 10) {
            echo $checkNumber;
            echo "I'm sorry, but one of the phone numbers that you entered is invalid";
            echo '<br />';
            echo "Please click the back button, and try again";
            exit;
        }
    }
}

Posted: Sat Dec 24, 2005 1:40 pm
by josh
well validating it's all numbers and then checking the length is the same thing as specifying the length in the regex itself

Code: Select all

([0-9]{10})
where before he had a + (meaning one or more) you now have {10} (meaning exactly 10 of the previous wildcard)


the problem is with the

$phonenumber.$i part,
he is using the concatenation operator instead of the brackets
$phonenumber[$i]


actually thinking about it he is probably not using arrays...

do you have your variables stored like:

$phonenumber1
$phonenumber2

?

this is getting into variable variables and I think arrays would best suite this to avoid confusion.


please read http://www.php.net/array

anyways I think the syntax would be

$$'phonenumber'.$i

to access a variable called
$phonenumber1 where $i is 1

Posted: Sat Dec 24, 2005 2:33 pm
by RobertGonzalez
I agree with you, jshpro2. I think he should use arrays as well. They would be easier to walk through at least.

His original post showed that he was storing the data in a variable appended by the number ($phoneNumbers1, $phoneNumbers2, etc.) but I do believe that arrays would be better suited to this type of code.

Posted: Sat Dec 24, 2005 3:16 pm
by dwessell
Right on.. I come back from working with it, and look at all the tips.. Thanks a ton... Help like this makes learning a new language easier..

I had come to the same conclusion, and was working on two methods for using an array.

The first method, was before checking the data, dumping the variable into an array..

Something like

Code: Select all

$phoneArray = array();
	for($i = 0; $i < $numberTeams; $i++){
		$phoneArray[$i] = $$phonenumber.$i;
	}
Or just putting them in an array to begin with.. This is how I was gathering the data..

Code: Select all

for ($i = 0; $i < $numberTeams; $i++)
{
    echo "Phone Number #${$i}  ";
	echo '<input type="text" name="phoneNumbers'.$i.'" size="15"  maxlength="10"><br>';
}
Can you tell I like for loops? :-) While loops make me somewhat uncomfortable <G>..

I would welcome suggestions either way..

Thanks
David

Posted: Sat Dec 24, 2005 7:40 pm
by RobertGonzalez
You could also trying posting them as an array from your HTML. But using them as an array to loop through is the best idea.

Posted: Sat Dec 24, 2005 7:53 pm
by dwessell
I'm leaning towards option (1)... Taken the $phoneNumber1, $phoneNumber2 etc.. and dumping them into an array before checking them.. If I can just get that syntax right that is.. :-)

Posted: Sun Dec 25, 2005 10:05 am
by dwessell
Hey guys... Problem all solved.. I ended up taking the already existing values, and dumping them into an array..

Thanks for the help!!

Posted: Sun Dec 25, 2005 11:08 pm
by RobertGonzalez
Make sure to edit the title of your original post and add a "[ SOLVED ] - " in front of the title.