function MaskPhon( obj ) {
obj.value = PhonNumStr( obj.value );
}
function PhonNumStr( str ){
var RegNotNum = /[^0-9]/g;
var RegPhonNum = "";
var DataForm = "";
// return blank
if( str == "" || str == null ) return "";
// delete not number
str = str.replace(RegNotNum,'');
if( str.length < 4 ) return str;
if( str.length > 3 && str.length < 7 ) {
DataForm = "$1-$2";
RegPhonNum = /([0-9]{3})([0-9]+)/;
} else if(str.length == 7 ) {
DataForm = "$1-$2";
RegPhonNum = /([0-9]{3})([0-9]{4})/;
} else if(str.length == 9 ) {
DataForm = "$1-$2-$3";
RegPhonNum = /([0-9]{2})([0-9]{3})([0-9]+)/;
} else if(str.length == 10){
if(str.substring(0,2)=="02"){
DataForm = "$1-$2-$3";
RegPhonNum = /([0-9]{2})([0-9]{4})([0-9]+)/;
}else{
DataForm = "$1-$2-$3";
RegPhonNum = /([0-9]{3})([0-9]{3})([0-9]+)/;
}
} else if(str.length > 10){
DataForm = "$1-$2-$3";
RegPhonNum = /([0-9]{3})([0-9]{4})([0-9]+)/;
}
while( RegPhonNum.test(str) ) {
str = str.replace(RegPhonNum, DataForm);
}
return str;
}
2. 입력박스
전화번호 : <input type='text' maxlength='13' onkeyup ='MaskPhon(this);' onkeydown='MaskPhon(this);' >