How to Convert IP to Decimal Using JavaScript With HTML With Example?

Hello Friends Today, through this tutorial, I will tell you How to IP to Decimal Convert Using JavaScript Without Submit Button with HTML? You can create an IP to Decimal Converter in JavaScript without using a submit button and dynamically update the result as the user types. Below is an example HTML and JavaScript code for such a converter:

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 Decimal 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;
}
p {
margin-top: 0;
}
</style>
</head>
<body>
<h2>IP to Decimal Converter</h2>
<label for="ipAddress">Enter IP Address:</label>
<input type="text" id="ipAddress" placeholder="Enter IP address">
<p id="decimalResult"></p>

<script>
// Function to convert IP to Decimal
function ipToDecimal(ip) {
const parts = ip.split('.');
if (parts.length === 4 && parts.every(part => parseInt(part, 10) >= 0 && parseInt(part, 10) <= 255)) {
const decimal = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
return decimal >>> 0; // Ensure it's an unsigned 32-bit integer
} else {
return 'Invalid IP address';
}
}

// Function to update result
function updateResult() {
const ipAddress = document.getElementById('ipAddress').value;
const decimalResult = ipToDecimal(ipAddress);
document.getElementById('decimalResult').innerText = `Decimal: ${decimalResult}`;
}

// Attach event listener to input
document.getElementById('ipAddress').addEventListener('input', updateResult);

</script>
</body>
</html>

This example includes an input field for entering the IP address, and the `updateResult` function is triggered whenever the user types into the input field. The `ipToDecimal` function is responsible for converting the IP address to its decimal representation. The result is then dynamically updated in a paragraph (`<p>`) element on the page.

Example:-

IP to Decimal Converter

IP to Decimal Converter