Forum Moderators: coopster

Message Too Old, No Replies

Phone numbers and forms, validation, mysql insert

phone numbers, validation, mysql insert

         

onawire

8:47 pm on Jul 22, 2010 (gmt 0)

10+ Year Member



I have a form - within it i have this phone number entry:

<table>
<tbody>
<tr>
<td>
<input type="text" name="PHONE1" size="1" onKeyUp="autoTab(this,
'PHONE2');" maxlength="3">
</td>
<td>
<input type="text" name="PHONE2" size="1" onKeyUp="autoTab(this,
'PHONE3');" maxlength="3">
</td>
<td>
<input type="text" name="PHONE3" size="1" onKeyUp="autoTab(this,'EMAIL')" maxlength="4">
</td>
</tbody>
</table>

Just US numbers are needed!

Question 1

How do i get mysql to take all three form boxes as ONE entry?

can i set:

$PHONE = PHONE1 + PHONE2 + PHONE3

then, have php send it off into mysql under the column 'PHONE'?

Question 2

How do i validate each phone number box?

lostdreamer

9:10 am on Jul 23, 2010 (gmt 0)

10+ Year Member



First you would need to validate each phone number seperately, something like this should do the trick:

//Phone Number (North America)
//Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.
//Replaces all those with (333) 444-5555
$phone1 = preg_replace('\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})', '(\1) \2-\3', $_POST['PHONE1']);
$phone2 = preg_replace('\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})', '(\1) \2-\3', $_POST['PHONE2']);
$phone3 = preg_replace('\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})', '(\1) \2-\3', $_POST['PHONE3']);

(If your phone numbers are different that above you can google for "php regular expression phone" to get other expressions.)

Then you concatenate the phone numbers (I would suggest using seperate table fields, but you could also use a delimiter for it and put it in 1 field)

$phones = $phone1 ."|". $phone2 ."|". $phone3;
// $phones = "0123456789|0123456789|0123456789";


Then insert that in the DB as you would with any variable.


Regards,
LostDreamer

lostdreamer

9:16 am on Jul 23, 2010 (gmt 0)

10+ Year Member



Woops,

I just saw I misread your question, I thought you had 3 seperate phone numbers...

You can just concatenate the 3 parts of the phonenumber like the following, and check the resulting phonenumber:

$phone = $_POST['PHONE1'].$_POST['PHONE2'].$_POST['PHONE3'];
// now make sure $phone only has numbers
$phone = preg_replace("|(^[0-9]|", "", $phone);
// check if it still has 10 digits
if(strlen($phone) < 10)
exit("Phonenumber is to short");

rocknbil

5:57 pm on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the interest of TMTOWTDI . . . .


$delimiter = '-';
list ($formatted_phone,$errs) = check_phone('PHONE',3,3,4,$delimiter);
if ($errs) {
// should actually return to form with error, but
echo "<p>Errors:</p></ul>$errs</ul>";
exit;
}
else { echo $formatted_phone; }


function check_phone($name,$len1,$len2,$len3,$delim=null) {
$phone_errors=$formatted=null;
// Field lengths, alter as needed
$flengths = Array($len1,$len2,$len3);
//
for ($i=1;$i<=3;$i++) {
$nm=$name.$i;
$index = $flengths[$i-1];
if (isset($_POST[$nm])) {
// Just cleanse it, errors confuse people,
// only error if you have to. A space, for example.
$_POST[$nm] = preg_replace('/[^\d]/','',$_POST[$nm]);
if (! (strlen($_POST[$nm])==$index)) {
$phone_errors .= "<li>Please enter $index numbers for phone field $i.</li>";
}
$formatted .= $_POST[$nm];
if ($i<3) { $formatted .= $delim; }
}
else { $phone_errors .= "<li>Phone field $i is blank.</li>"; }
}
return Array($formatted,$phone_errors);
}


If you just want the numbers and no delimiter, just don't define delimiter, it's optional in the function. Do
$delimiter = '';
list ($formatted_phone,$errs) = check_phone('PHONE',3,3,4,$delimiter);

or even
list ($formatted_phone,$errs) = check_phone('PHONE',3,3,4);

** may contain typos, not copy and paste code, go forth and debug **