스크립트
javascript 휴대폰 번호 자동완성
고.니
2020. 4. 3. 23:54
반응형
$("#mobileno").on("input keyup", function() {
var value = $(this).val();
if(value.length > 12) {
$(this).val(autoHypenNo([3,4,4],$(this).val()));
} else {
$(this).val(autoHypenNo([3,3,4],$(this).val()));
}
});
function autoHypenNo(arr, str){
str = str.replace(/[^0-9]/g, '');
var delimiter = '-';
var result = '';
var fromIndex = 0;
var strlen = str.length;
for(var i=0; i<arr.length; i++) {
var currentStrCnt = arr[i];
result += str.substr(fromIndex, currentStrCnt);
fromIndex += currentStrCnt;
if(strlen <= fromIndex) {
result += str.substr(fromIndex, strlen);
break;
}
if(i<arr.length-1) {
result += delimiter;
}
}
return result;
}
반응형