사이트 내 전체검색
[javascript] 전화번호 입력시 자동 하이픈 넣기
로빈아빠
https://cmd.kr/javascript/599 URL이 복사되었습니다.

본문

<script type="text/javascript">
function OnCheckPhone(oTa) {
var oForm = oTa.form ;
var sMsg = oTa.value ;
var onlynum = "" ;
onlynum = RemoveDash2(sMsg);
if(event.keyCode != 8 ) {
if (GetMsgLen(onlynum) <= 2) oTa.value = onlynum ;
if (GetMsgLen(onlynum) == 3) oTa.value = onlynum + "-";
if (GetMsgLen(onlynum) == 4) oTa.value = onlynum.substring(0,3) + "-" + onlynum.substring(3,4) ;
if (GetMsgLen(onlynum) == 5) oTa.value = onlynum.substring(0,3) + "-" + onlynum.substring(3,5) ;
if (GetMsgLen(onlynum) == 6) oTa.value = onlynum.substring(0,3) + "-" + onlynum.substring(3,6) ;
if (GetMsgLen(onlynum) == 7) oTa.value = onlynum.substring(0,3) + "-" + onlynum.substring(3,7) ;
if (GetMsgLen(onlynum) == 8) oTa.value = onlynum.substring(0,4) + "-" + onlynum.substring(4,8) ;
if (GetMsgLen(onlynum) == 9) oTa.value = onlynum.substring(0,4) + "-" + onlynum.substring(4,9) ;
}
}

function RemoveDash2(sNo) {
var reNo = ""
for(var i=0; i<sNo.length; i++) {
if ( sNo.charAt(i) != "-" ) {
reNo += sNo.charAt(i)
}
}
return reNo
}

function GetMsgLen(sMsg) { // 0-127 1byte, 128~ 2byte
var count = 0
for(var i=0; i<sMsg.length; i++) {
if ( sMsg.charCodeAt(i) > 127 ) {
count += 2
}
else {
count++
}
}
return count
}
</script>
<form>
<input type=text name=num1 value="011" size=4> -
<input type=text name=num2 onfocus="OnCheckPhone(this)" onKeyup="OnCheckPhone(this)" size=10>
</form>

댓글목록

등록된 댓글이 없습니다.

831 (7/17P)

Search

Copyright © Cmd 명령어 18.221.68.196