flemingmike Posted September 24, 2010 Share Posted September 24, 2010 hello, i have a text input box for phone numbers. i was wondering if there is a way to make it so when the users type in 9055559999 that it will display to them as 905-555-9999. Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/ Share on other sites More sharing options...
BlueSkyIS Posted September 24, 2010 Share Posted September 24, 2010 $phone = "9055559999"; $pretty_phone = substr($phone,0,3)."-".substr($phone,3,3)."-".substr($phone,6); echo "pretty_phone: $pretty_phone"; ; Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/#findComment-1115015 Share on other sites More sharing options...
flemingmike Posted September 24, 2010 Author Share Posted September 24, 2010 thanks, i know how to do it after the fact, i was wondering about while they type though. Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/#findComment-1115017 Share on other sites More sharing options...
BlueSkyIS Posted September 24, 2010 Share Posted September 24, 2010 i am an idiot. this is the javascript forum. doh. i thought i was answering php questions. Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/#findComment-1115018 Share on other sites More sharing options...
Adam Posted September 24, 2010 Share Posted September 24, 2010 Is the format always nnn-nnn-nnnn ? Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/#findComment-1115087 Share on other sites More sharing options...
JonnoTheDev Posted September 24, 2010 Share Posted September 24, 2010 <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> <script type="text/javascript"> function formattel() { var element = document.getElementById("tel"); element.onkeypress = function() { var length = element.value.length; if(length == 9) { var reg = new RegExp("^[-]?[0-9]+[\.]?[0-9]+$"); if(reg.test(element.value)) { var tel = element.value; element.value = tel.substring(0,3)+"-"+tel.substring(3,6)+"-"+tel.substring(6,10); } } } } window.onload = function() { formattel(); } </script> </head> <body> Tel: <input type="text" name="tel" id="tel" maxlength="12" size="12" /> </body> </html> Link to comment https://forums.phpfreaks.com/topic/214277-can-i-auto-make-in-phone-field/#findComment-1115137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.