본문 바로가기

programming/javascript

input 전화번호 입력 받기 (정규표현식)

정규표현식 이용해서 input 에 전화번호 입력 받기 예제입니다~ 

문자는 입력하면 사라지게 됩니다

 

 

 

 

코드

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<h3>전화번호 입력 받기</h3>
<input id="input_phone" type="text" placeholder="- 없이 입력해주세요">

<script>
    const inputPhone = document.getElementById('input_phone');

    inputPhone.addEventListener('keyup', (e) => {
        let inputValue = e.target.value;
        inputPhone.value = inputValue.replace(/[^0-9]/gi, '');
    });

</script>

</body>
</html>