js
[HTML] input ํ๊ทธ์ ์ซ์๊ฐ๋ง ๋ฐ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ
natrue
2021. 12. 29. 15:20
728x90
1. oninput ์ด๋ฒคํธ, ์ ๊ท์, replace() ํจ์ ํ์ฉ
oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"
<th>์ฐ๋ฝ์ฒ</th>
<td>
<input type="text" id="info01" name="info04" value="" maxlength="3" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
<span class="date">-</span>
<input type="text" id="info02" name="info05" value="" maxlength="4" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
<span class="date">-</span>
<input type="text" id="info03" name="info06" value="" maxlength="4" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
</td>
</tr>
2. input type "number"
<input type="number">
<th>์ฐ๋ฝ์ฒ</th>
<td>
<input type="number" id="info01" name="info04" value="" maxlength="3">
<span class="date">-</span>
<input type="number" id="info02" name="info05" value="" maxlength="4">
<span class="date">-</span>
<input type="number" id="info03" name="info06" value="" maxlength="4">
</td>
</tr>
3. ์ ๋ ฅ๋ keycode ์ฒดํฌํ๊ธฐ
<input type="text" onkeypress="return checkNumber(event)">
function checkNumber(event) {
if(event.key === '.'
|| event.key === '-'
|| event.key >= 0 && event.key <= 9) {
return true;
}
return false;
}
์ถ์ฒ: https://hianna.tistory.com/413