Hello Friends Today, through this tutorial, I will tell you How to IP to Binary Convert Using JavaScript Without Submit Button with HTML? You can create a simple IP to Binary Converter using JavaScript with HTML without a submit button. Below is an example:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP to Binary Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 16px;
box-sizing: border-box;
}
</style>
</head>
<body>
<h2>IP to Binary Converter</h2>
<label for="ipAddress">Enter IP Address:</label>
<input type="text" id="ipAddress" placeholder="Enter IP address" oninput="convertToBinary()">
<h3>Binary Representation:</h3>
<p id="binaryResult"></p>
<script>
function convertToBinary() {
// Get the IP address input value
const ipAddress = document.getElementById('ipAddress').value;
// Validate the IP address
if (/^(\d{1,3}\.){3}\d{1,3}$/.test(ipAddress)) {
// Split the IP address into octets
const octets = ipAddress.split('.');
// Convert each octet to binary
const binaryArray = octets.map(octet => {
const binary = parseInt(octet, 10).toString(2).padStart(8, '0');
return binary;
});
// Display the binary result
document.getElementById('binaryResult').innerHTML = binaryArray.join('.');
} else {
// Display an error message for invalid input
document.getElementById('binaryResult').innerHTML = 'Invalid IP address';
}
}
</script>
</body>
</html>
This HTML file includes an input field for entering an IP address. As the user types, the `convertToBinary` function is triggered, which converts the IP address to its binary representation and updates the result on the page. The input is validated to ensure it matches the format of a typical IP address. If the input is invalid, an error message is displayed.