본문 바로가기
웹프로그래밍/HTML5

[HTML5] 애러캐치

by 폴더맨 2024. 6. 9.
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript try catch</h2>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
  const message = document.getElementById("p01");
  message.innerHTML = "";
  let x = document.getElementById("demo").value;
  try { 
    if(x.trim() == "")  throw "empty";
    if(isNaN(x)) throw "not a number";
    x = Number(x);
    if(x < 5)  throw "too low";
    if(x > 10)   throw "too high";
  }
  catch(err) {
    message.innerHTML = "Input is " + err;
  }
}
</script>

</body>
</html>

'웹프로그래밍 > HTML5' 카테고리의 다른 글

[HTML5] picture  (0) 2024.06.10
[HTML5] 타이틀 이미지 넣기  (0) 2024.06.09
[HTML5] picture  (0) 2024.06.09
[HTML5] output  (0) 2024.06.09
[HTML5] position z-index  (0) 2024.06.09