PHP - Please Help With Us Phone Number Validation
I have a form in which I want it to accept US phone formats, ie (123) 123-1234, 123-123-1234, 1-123-123-1234, etc..
Below is my code and I would really appreciate it if someone can tell me what I need to accept the #s. Thank you! $session = $_SESSION['verify']; $error = ''; if(trim($name) == '') { $error .= '<li>Your name is required.</li>'; } if(trim($email) == '') { $error .= '<li>Your e-mail address is required.</li>'; } elseif(!isEmail($email)) { $error .= '<li>You have entered an invalid e-mail address.</li>'; } if(trim($phone) == '') { $error .= '<li>Your phone number is required.</li>'; } elseif(!is_numeric($phone)) { $error .= '<li>Your phone number can only contain digits.</li>'; } if(trim($comments) == '') { $error .= '<li>You must enter a message to send.</li>'; } if($session != $verify) { $error .= '<li>The verification code you entered is incorrect.</li>'; } if($error != '') { echo '<div class="error_message">Attention! Please correct the errors below and try again.'; echo '<ul class="error_messages">' . $error . '</ul>'; echo '</div>'; } else { if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); } Similar TutorialsHi All, I only want to allow phone number in ###-###-####. Anything else I want an error. Here is what I have, which says any number is valid. Function: function checkPhone($number) { if(preg_match('^[0-9]{3}+-[0-9]{3}+-[0-9]{4}^', $number)) { return $number; } else { $items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/'); $clean = preg_replace($items, '', $number); return substr($clean, 0, 3).'-'.substr($clean, 3, 3).'-'.substr($clean, 6, 4); } } Checking Number: $number = '1231231234'; if(checkPhone($number)) { echo $number.' is a valid phone number.'; } else { echo $number.' is not a valid phone number.'; } This should give me the not valid message. Thanks Alright so I am writing up a function to verify various formats of US based phone numbers.. And I seemed to have run into a snag. This is what I have gotten so far, and for the most part works well. Code: [Select] function validate_phoneUS($number){ $numStripX = array('(', ')', '-', '.', '+'); $numCheck = str_replace($numStripX, '', $number); $firstNum = substr($number, 0, 1); if(($firstNum == 0) || ($firstNum == 1)) {return false;} elseif(!is_numeric($numCheck)){return false;} elseif(strlen($numCheck) > 10){return false;} elseif(strlen($numCheck) < 10){return false;} else{ $formats = array('###-###-####', '(###) ###-####', '(###)###-####', '##########', '###.###.####', '(###) ###.####', '(###)###.####'); $format = trim(ereg_replace("[0-9]", "#", $number)); return (in_array($format, $formats)) ? true : false; } } When I try to run these test numbers down the line.. Code: [Select] $number1 = '0234567890'; $number2 = '1234567890'; $number3 = "(555)555-5555"; $number4 = '555-555-5555'; $number5 = '555.555.5555'; $number6 = '5555555555'; $number7 = "(555) 555-5555"; I get these as a result.. 0234567890 is bad. 1234567890 is bad. (555)555-5555 is bad. 555-555-5555 is a valid phone number. 555.555.5555 is a valid phone number. 5555555555 is a valid phone number. (555) 555-5555 is bad. Where I am stuck is on the numbers that have the parenthesis wrapped around them. For a while I had it working with them and coming back as a valid number, and then I changed a couple things else where for something else. and now they come back as bad. I am getting tired, and I need some extra eyes to help me figure this out.. what did I snag myself on? hello, i cant find this anywhere on the internet. i dont even know what to search. if you are on your phone and there is a phone number on a web page, sometimes you can click on that number and it will dial it. how can i do that? How to text a phone number in database as well as record recieved textmessages into mysql database. Any help here? i've been googling for ages. I know this should be easy.... However, to validate a 7-digit phone number, I wrote this: Code: [Select] <?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 hi friends could someone help with this criteria? i want to restrict entry to db if the post value number was entered into db less than 31 days ago?
if($homePhone == '') { $qry = "SELECT * FROM table WHERE homePhone='$homePhone '";in db i store time in datetime current timestamp like 2013-07-13 21:19:15 could someone help me out? Edited by lovephp, 12 July 2014 - 11:36 AM.
Hi,
<?php $connect = mysqli_connect('localhost', 'root', '1234', 'contact'); $query ='SELECT * FROM tbl_employee ORDER BY ID DESC'; $result = mysqli_query($connect, $query); ?>
<table id="employee_data" class="table table-striped table-bordered"> <thead> <tr> <td>Name</td> <td>Location</td> <td>Department</td> <td>Phone Number</td> </tr> </thead> <?php while($row = mysqli_fetch_array($result)) { echo ' <tr> <td>'.$row["name"].'</td> <td>'.$row["location"].'</td> <td>'.$row["department"].'</td> <td>'.$row['phone_number'].'</td> </tr> '; } ?> </table>
I have to do an assignment for school where I create an html page and a separate page for php. The code is to sum two numbers and return an error if the value entered is not numeric. The sum portion is working; however the numeric validation is not. The code is he
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Enter your information in the form below</title> </head> <body> <!-- Form2.html --> <form action="Form2.php" method="post"> <fieldset><legend> Enter a number in the form below:</legend> <p><b>number 1: <input type="text" name="number1" size="4" maxlength="4" /></b></p> <p><b>number 2: <input type="text" name="number2" size="4" maxlength="4" /></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> </form> </body> </html> <html> <body> <?php $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sum = $number1+$number2; if (!empty($number1)&& !empty($number2)) { if(!is_numeric($number1)) { echo "The value you entered for number 1 is not a number. Please enter a numeric value for number 1"; } else if(!is_numeric($number2)) { echo "The value you entered for number 2 is not a number. Please enter a numeric value for number 2"; } else { echo "The sum of the numbers is: ".$sum; } } ?> </body> </html> Can anyone tell me why this bit of coding isn't working. I want a a form to validate the telephone number for the UK and been having a bash around with various ways to code this, with no success so far. I started with FILTER_VALIDATE_INT, but found out that "0" wouldnt be taken so I added FILTER_FLAG_ALLOW_OCTAL. But then it seems that the spaces, dashes wouldn't be taken as well as a limit on 10 characters occured. So I tried to write myself a small function that used an array to validate the styles etc... But this seemed to allow any string character. So I moved on to preg_match, but I can't seem toget my coding to work. It's driving me mad now, probably just need a break But any help will be much appericated! [ if (!empty($_SESSION['add_customer_form'] ["telephone_no"])) { //if (!is_numeric($number)) //if(!filter_var($int, )) $number = $_POST["telephone_no"]; if(!preg_match("/^(+)?(([0-9]+)-?s?)*([0-9]+-[0-9]+)*([0-9]+)*$/", trim($number))) { $_SESSION['add_customer_errors']["telephone_no"] = ("$number Sorry, but you′ve entered an incorrect fromat"); } //if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $_SESSION['add_customer_errors']["telephone_no"]) ) //{ /// $number = $_POST["telephone_no"]; //$_SESSION['add_customer_errors']["telephone_no"] = ("$number Sorry, but you′ve entered an incorrect fromat"); //} } ] I'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? Hello,
I have problem durring binding update query. I can't find what is causing problem.
public function Update(Entry $e) { try { $query = "update entry set string = $e->string,delimiter=$e->delimiter where entryid= $e->id"; $stmt = $this->db->mysqli->prepare($query); $stmt->bind_param('ssi',$e->string,$e->delimiter,$e->id); $stmt->close(); } catch(Exception $ex) { print 'Error: ' .$ex->getMessage(); } }When I run function update I'm getting next error:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Can you help me to solve this problem ? Edited by danchi, 17 October 2014 - 10:25 AM. I need to display a number(the number is retrieved from the db) in the form input field such that only the last 4 digits is visbile, the remaining can be masked as * or X or whatever is applicable. I know the last 4 can be obtained as follows: Code: [Select] $number=substr($number,-4,4); But when i hit the submit button the form validates the input field and checks if the number is a valid number of a specific format. Therefore when I click on the submit button then I should still be able to unmask the masked numbers or do something similar that would help me validate the whole number. Code: [Select] <input type="text" name="no" value="<?php if(!empty($number)){ echo $number;} ?>"> Hi All, I guess there must be someway to do this with regex, but I'm not too hot on regex. What I want to do is check a text area for the presence of a phone number, and if it is there remove it. Ideally I don't want to remove all number as there might be a need for them to use a number, for example... I am 25 years old would be fine, but entering their number wouldn't be. Is there anyway of tacking this with regex, or is it going to be tricky? Many thanks, Greens85 Today, Amazon announced the "Fire Phone", what are your thoughts?
http://www.businessi...re-phone-2014-6
Hi forum, I would like to create a webpage (no DB) where I internally can reboot my phones (with https+login ofcause...). This is my reboot.php from http://code.google.com/p/php-sip/: --- <?php require_once('PhpSIP.class.php'); try { $api = new PhpSIP('172.30.30.1'); // IP we will bind to $api->setUsername('10000'); // authentication username $api->setPassword('secret'); // authentication password // $api->setProxy('some_ip_here'); $api->addHeader('Event: resync'); $api->setMethod('NOTIFY'); $api->setFrom('sip:10000@sip.domain.com'); $api->setUri('sip:10000@sip.domain.com'); $res = $api->send(); echo "res1: $res\n"; } catch (Exception $e) { echo $e->getMessage()."\n"; } ?> --- What I would like to achieve is to have a index.php with a "SITE" dropdown and a multiselect "PHONE" dropdown - where only marked phone(s) for a selected site can be chosen (most likely some java stuff...?): I need to be able to run this (reboot.php) from linux shell (like: php reboot.php <IP>) so I can schedule remote phonereboots when software has been updated on my provisioning server. It would be awesome if I was also able to choose "SITE"+"ALL" and reboot all phones in a site from both webpage and shell... Thanks in advance :-) ! ~Maymann Can someone advise the edit required to include validation of US phone numbers ($phone_number3) when entered with no spaces between the area code and/or prefix. Note: I have working code (regular expression) that validates phone numbers that enclose the area code with parenthesis or having a space, hyphen, or dot between area code, prefix, or last four digits. Examples: all three shown below are validated based on regular expresion (123) 456-7890 123.456.7890 123 456 7890 However, I want to also include the option for 1234567890 (phone number have NO spaces) as valid. I know the edit will be within ( |-|.) using the PIPE but not sure how to denote the "no space" syntax within the regular expression. See the regular expression: $good_phone = "^\(?[0-9]{3}\)?( |-|.)[0-9]{3}(-|.)[0-9]{4}$"; and if you wan to view the entire php snippet // function which validates an American phone number function validate_phone($phone_number) { $good_phone = "^\(?[0-9]{3}\)?( |-|.)[0-9]{3}(-|.)[0-9]{4}$"; if (preg_match("/$good_phone/", $phone_number)) { echo "$phone_number is valid.<br/>"; } else echo "$phone_number is NOT valid.<br/>"; } $phone_number1 = "123.456.7890"; validate_phone($phone_number1); $phone_number2 = "(123) 456-7890"; validate_phone($phone_number2); $phone_number3 ="1234567890"; validate_phone($phone_number3); This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=310894.0 Dear Members, I am a PHP developer and desisgning a WAP page for mobiles.I need some php code to find out the model of a mobile phone from where request is coming so that based on that information I can upload the proper CSS for my page.If you can give me some hints as well that would also help me to proceed further.Currently I am able to fetch user-agent of mobile but not able to find out the specific model of mobile. First off, I am new to PHP, and want to learn it, but it seems very complicated. Where do I begin? I found a script online for a contact form and I am having trouble making it work for my needs. I wanted to include a phone number field in the email message. This is what I have so far for the PHP, but it does not include the phone field in the body of the message. Everything else works fine. Apologize in advance if this message has been posted many times before. <?php $post = (!empty($_POST)) ? true : false; if($post) { include 'functions.php'; $name = stripslashes($_POST['contactname']); $email = trim($_POST['email']); $subject = "New Message from your website"; $message = stripslashes($_POST['message']); $phone = stripslashes($_POST['phone']); $error = ''; // Check name if(!$name) { $error .= 'Please enter your name.<br />'; } // Check email if(!$email) { $error .= 'Please enter an e-mail address.<br />'; } if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; } // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } ?> Hello folks, So, not sure where to start, but I'm wondering if it's at all possible to change a prepared statement / sql based on whether or not we're displaying for desktops, or for smartphone/tablets? I'm not sure what to start looking at since I'm pretty new in PHP and all... I've managed to figure out how to change CSS if I'm on phone @media only screen and (max-width: 767.98px){ // PUT CSS FOR PHONES... } Any directions on how I would change my SQL, would be awesome! I'd like to narrow down the columns returned if I'm on phone, otherwise breaks my pages looks too much... Much thanks in advance! Pat
|