In this article, we are going to check if a number is odd or even. We have many methods to do this task but we use one of the easiest methods.

HTML
<!DOCTYPE html>
<html>
<head>
<title>Check If a Number is Odd or Even</title>
</head>
<body>
<form>
<div class="mb-3">
<label for="number" class="form-label">Enter Number</label>
<input type="text" class="form-control" id="number">
</div>
<hr>
<h4>The Entered Number is: <span id="result"></span></h4>
</form>
<input type="button" value="Check" onclick="myFunction()" />
</body>
</html>
Javascript
Here is using the modulus % operator to determine if a number is odd or even in JavaScript.
function myFunction() {
var num = document.getElementById("number").value;
if(!/^[0-9]+$/.test(num)){
alert("Please only enter numeric characters (Allowed input:0-9)")
}else{
if(num % 2 == 0) {
var result = "Even!";
} else {
var result = "Odd!";
}
document.getElementById("result").innerHTML = result;
}
}
I make numeric character validation so users can enter an only valid numeric values.

Brijpal Sharma is a web developer with a passion for writing tech tutorials. Learn JavaScript and other web development technology.