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