BCAV_WEB Posted November 17, 2010 Share Posted November 17, 2010 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"); //} } ] Quote Link to comment Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 Country code - necessary only when dialing to phones in other countries. In international usage, telephone numbers are prepended with the country code preceded by a "+", and with spaces in place of hyphens (e.g., "+XX YYY ZZZ ZZZZ"). This allows the reader to choose which Access Code (also known as International Dialing Digit) they need to dial from their location. However, it is often quoted together with the international access code which must precede it in the dial string, for example "011" in NANP countries (including Canada, Bermuda, and the United States): "011-XX-YYY-ZZZ-ZZZZ", or "00" in most European countries: "00-XX-YYY-ZZZ-ZZZZ". This can cause confusion as a different Access Code may be used where the reader is located. On GSM networks, "+" is an actual character that may be used internally as the international access code, rather than simply being a convention. when working with regex's always important to look at how to fit the pattern Hardest part about this pattern is the initial +/011/00 country designator but i think i worked it out (but no () parens around area code for local) <?php $numbers=array( '234-567-8901', '+01 234 567 8901', '011-01-234-567-8901', '00-01-234-567-8907', '0123456789' ); foreach($numbers as $val) { echo $val . " is ". (preg_match('@^((\+|((011|00)(\s|-)))\d{2,2}(\s|-))?\d{3,3}(\s|-)\d{3,3}(\s|-)\d{4,4}$@',$val)?'':'not ') . "ok<br />\r\n"; } ?> Wow, that regex is mad looking Quote Link to comment Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 forgot long distance format in the preg, just replace the pattern with: ^(((\+|((011|00)(\s|-)))(\d{2,2})|1)(\s|-))?\d{3,3}(\s|-)\d{3,3}(\s|-)\d{4,4}$ Quote Link to comment Share on other sites More sharing options...
BCAV_WEB Posted November 17, 2010 Author Share Posted November 17, 2010 Thinking about it, im not sure if that will work; as I'm doing this for a UK telephone number so it wuld be like 0161 666 6666 01706 666 6666 033 666 6666 Quote Link to comment Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 thats a much simpler match string: ^\d{3,5}(\s|-)\d{3,3}(\s|-)\d{4,4} Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 I would suggest not validating the "format" of the phone number and instead validate that the phone number has the correct number of digits. Then you can store just the numbers in your database and use a consistent manner in which to display the phone number. For example (using US numbers), I would allow the user to enter in any value that consisted of 10 numeric digits OR 11 digits if the first number was a 1: - 123-465-7890 - (123) 456-789 - 123.456.7890 - 1-123-456-7890 Sampe code for validating function validPhone($phone) { //Remove all non digits $phone = preg_replace('/[^\d]/', '', $phone); //Test number of digits if(preg_match('/^1?\d{10}$/', $phone) !=1 ) { return false; } return $phone; } That function will return false if the number doesn't contain a valid number of digits, else it returns just the digits of the number. So, if validation passes I store just the numeric digits of the entered value. Then, whenever I need to display the value on the page I would have a function to format the phone numbers in a consistent format. function formatPhone($phone) { if(!$phone) { return false; } $format = (strlen($phone)==10) ? '(\2) \3-\4' : '\1 (\2) \3-\4'; return preg_replace('/(\d)(\d{3})(\d{3})(\d{4})/', $format, $phone); } Quote Link to comment Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 You are too funny mj sumtimes the best answers are the easiest to implement... Yeah, I like that number validation technique over the formmated technique. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.