입력이 숫자인지 문자인지 확인하려면 JavaScript의 isNaN() 함수를 사용하세요. 값이 NaN, 즉 숫자가 아닌 경우 true를 반환합니다. 다음은 코드입니다 -
예시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.
css" integrity="sha384- BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
</head>
<body>
<form name="checkingInput" action="" onsubmit="return
checkInputIsNumber()">
Enter the value: <input type="text" name="txtValue">
<br>
<input type="submit" value="check">
<script>
function checkInputIsNumber(){
var value=document.forms["checkingInput"]["txtValue"].value;
if (isNaN(value)){
alert("Please Provide the input as a number");
return false;
}
}
</script>
</body>
</html> 위의 프로그램을 실행하기 위해서는 "anyName.html(index.html)"이라는 파일명을 저장하고 파일을 우클릭하면 됩니다. VS Code 편집기에서 "Open with Live Server" 옵션을 선택합니다.
출력
이것은 다음과 같은 출력을 생성합니다 -

숫자를 제외한 값을 제공하면 경고 메시지가 표시됩니다. 이것은 다음과 같은 출력을 생성합니다 -

여기에 값 100을 입력합니다. 스크린샷은 다음과 같습니다 -

확인 버튼을 클릭하면 -
와 같은 쿼리 문자열로 값이 표시됩니다.?txtValue=yourValue.
이것은 다음과 같은 출력을 생성합니다 -
