Hello Friends Today, through this tutorial, I will tell you How to IP to Octal Convert Using JavaScript Without Submit Button with HTML? You can create an IP to Octal Converter using JavaScript and HTML without a submit button. Here’s a simple 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 Octal 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 {
font-weight: bold;
margin-top: 16px;
}
</style>
</head>
<body>
<h2>IP to Octal Converter</h2>
<label for="ipAddress">Enter IP Address:</label>
<input type="text" id="ipAddress" placeholder="Enter IP Address" oninput="convertToOctal()">
<p id="result"></p>
<script>
function convertToOctal() {
// Get the IP address input value
const ipAddress = document.getElementById('ipAddress').value;
// Validate the IP address format
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
if (!ipRegex.test(ipAddress)) {
document.getElementById('result').innerHTML = 'Invalid IP Address';
return;
}
// Split the IP address into octets
const octets = ipAddress.split('.').map(Number);
// Convert each octet to octal
const octalOctets = octets.map(octet => octet.toString(8));
// Display the result
document.getElementById('result').innerHTML = `Octal Representation: ${octalOctets.join('.')}`;
}
</script>
</body>
</html>
This HTML file includes an input field for entering an IP address, and a paragraph (`<p>`) to display the octal representation of the IP address. The `convertToOctal` function is triggered whenever the input changes (`oninput` event) and performs the conversion. The input is validated to ensure it matches a typical IP address format. The result is then displayed on the page.