____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
I'm just a die-hard RegEx user so I usually point to those references. I'll remember the FAQ though from now on.
____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
Probably this will help. I know the code is long for just the formatting however I didn't want to sacrifice the understanding of the idea for a more compact code.
If you need to modify and make it more suitable for your project please do.
The Alert() is thre only for debugging purposes you can either comment that out or just delete it.
function formatphone(digits,format){
var txtphone;
var txtfirst=digits.substr(0,3);
var txtmiddle=digits.substr(3,3);
var txtlast=digits.substr(6,4);
switch (format){
case 0:
//have a format here
break;
case 1:
//have another format here
break;
case 2:
txtphone="("+txtfirst+""+txtmiddle+"-"+txtlast;
break;
}//endswitch
return txtphone;
}
function checkphone(elem){
var phoneFormats=new Array();
phoneFormats[0]=/^\d{10}$/; //1234567890
phoneFormats[1]=/^\(\d{3}\)\d{3}\-\d{4}$/; //(123)456-7890
phoneFormats[2]=/^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
phoneFormats[3]=/^\d{3}\.\d{3}\.\d{4}$/; //123.456.7890
// you can add as many different formats and combinations as you want by adding elements to the array.
var boophone=false;
for(var i=0;i<phoneFormats.length;i++){
if(!boophone){
alert('The phone number is incorrect');
elem.select();
elem.focus();
}else{
//do any kind of formatting here. even call a function that does the formatting for you.
elem.value=formatphone(elem.value.replace(/[^\d]/g,''),2);//pass only the digits and from there let the function format the phone based in an index of your preference
}
nice post!
about the only thing I would do is imbed a (\s) replace for spaces in the patterns. Most people in the US are going to format it (222) 333-4444 note the space between the ) 3. this will fail the tests on /^\(\d{3}\)\d{3}\-\d{4}$/;
something like this
function checkphone(elem){
alert(elem.replace(/\s/gi,"");
var phoneCheck = /^\(\d{3}\)\d{3}\-\d{4}$/.test(elem.replace("\s","");
}
if(!checkphone("\(222\) 333-4444") {
alert("Phone format OK!"
} else {
alert("Phone format NOT OK!"
}
____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
um.. looks like my quick type has some backwards logic
____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
correction to the pattern for repalce of spaces
elem.replace(/\s/g,""
____________________________________________________ get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
you are correct.
Not only I forgot to do that but also a general replace of white spaces
for example
123 456 7890
123. 456. 7890
It's my personal belief that all of them should pass the test and be considered as valid phone number.
As usual I got caught in the formatting for tek-tips and the name of the index "i". please replace the previous code I posted with this new one.
again thos bolded alerts are for debugging only.
<script type="text/javascript">
function formatphone(digits,format){
var txtphone;
var txtfirst=digits.substr(0,3);
var txtmiddle=digits.substr(3,3);
var txtlast=digits.substr(6,4);
switch (format){
case 0:
//have a format here
break;
case 1:
//have another format here
break;
case 2:
txtphone="("+txtfirst+""+txtmiddle+"-"+txtlast;
break;
}//endswitch
return txtphone;
}
function checkphone(elem){
var phoneFormats=new Array();
phoneFormats[0]=/^\d{10}$/; //1234567890
phoneFormats[1]=/^\(\d{3}\)\d{3}\-\d{4}$/; //(123)456-7890
phoneFormats[2]=/^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
phoneFormats[3]=/^\d{3}\.\d{3}\.\d{4}$/; //123.456.7890
// you can add as many different formats and combinations as you want by adding elements to the array.
var boophone=false;
var txtphone=elem.value.replace(/\s/g,''); alert('phone: |'+txtphone+'|');
for(var index=0;index<phoneFormats.length;index++){
if(!boophone){
alert('The phone number is incorrect');
elem.select();
elem.focus();
}else{
//do any kind of formatting here. even call a function that does the formatting for you.
elem.value=formatphone(elem.value.replace(/[^\d]/g,''),2);//pass only the digits and from there let the function format the phone based in an index of your preference
}
}//endfunc
</script>
grtfercho çB^]\.. "Imagination is more important than Knowledge" A. Einstein
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.