๐Ÿ“ŒLanguage/JavaScript

input ํƒœ๊ทธ ์ž˜ ํ™œ์šฉํ•˜๊ธฐ valueAsNumber

hellohailie 2024. 11. 30. 16:20
๋ฐ˜์‘ํ˜•

 

input ํƒœ๊ทธ์˜ type='number' ์˜ ํŠน์„ฑ์„ ์ž˜ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

=> valueAsNumber ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

๊ธฐ์กด ์ฝ”๋“œ

const inputAmount = Number.parseInt(elements.moneyInput.value);

  if (!inputAmount || inputAmount <= 0) {
    addLog('์˜ฌ๋ฐ”๋ฅธ ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
    return;
  }

 

valueAsNumber ์‚ฌ์šฉ ์ฝ”๋“œ

const inputAmount = elements.moneyInput.valueAsNumber;

  if (Number.isNaN(inputAmount) || inputAmount <= 0) {
    addLog('์˜ฌ๋ฐ”๋ฅธ ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
    return;
  }

 

 

์žฅ์ 

- input ๊ฐ’์„ ์ˆซ์ž๋กœ ์ง์ ‘ ๊ฐ€์ ธ์™€์„œ ๋ถˆํ•„์š”ํ•œ ํ˜•๋ณ€ํ™˜ ๊ณผ์ •์ด ์—†์–ด์ง„๋‹ค. 

 

์ฃผ์˜

- valueAsNumber์€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ NaN์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์‹œ ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. 

Number.isNaN()

 

๋ฐ˜์‘ํ˜•