<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Subnet Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #2f2f2f;
color: #fff;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #3f3f3f;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.input-group {
margin-bottom: 20px;
}
.input-group label {
display: block;
margin-bottom: 10px;
}
.input-group input {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #4f4f4f;
color: #fff;
}
.result-group {
margin-bottom: 20px;
}
.result-group label {
display: block;
margin-bottom: 10px;
}
.result-group input {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #4f4f4f;
color: #fff;
}
.error {
color: #ff0000;
font-size: 12px;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>Subnet Calculator</h1>
<div class="input-group">
<label for="ip-address">IP Address:</label>
<input type="text" id="ip-address" placeholder="Enter IP Address (e.g., 192.168.1.1)">
<div class="error" id="ip-error"></div>
</div>
<div class="input-group">
<label for="cidr">CIDR Subnet Mask:</label>
<input type="text" id="cidr" placeholder="Enter CIDR (e.g., /24)">
<div class="error" id="cidr-error"></div>
</div>
<div class="result-group">
<label for="network-address">Network Address:</label>
<input type="text" id="network-address" readonly>
</div>
<div class="result-group">
<label for="broadcast-address">Broadcast Address:</label>
<input type="text" id="broadcast-address" readonly>
</div>
<div class="result-group">
<label for="total-hosts">Total Number of Hosts:</label>
<input type="text" id="total-hosts" readonly>
</div>
<div class="result-group">
<label for="usable-hosts">Number of Usable Hosts:</label>
<input type="text" id="usable-hosts" readonly>
</div>
<div class="result-group">
<label for="subnet-mask">Subnet Mask:</label>
<input type="text" id="subnet-mask" readonly>
</div>
<div class="result-group">
<label for="wildcard-mask">Wildcard Mask:</label>
<input type="text" id="wildcard-mask" readonly>
</div>
<div class="result-group">
<label for="ip-class">IP Address Class:</label>
<input type="text" id="ip-class" readonly>
</div>
<div class="result-group">
<label for="ip-type">IP Type:</label>
<input type="text" id="ip-type" readonly>
</div>
</div>
<script>
const ipInput = document.getElementById('ip-address');
const cidrInput = document.getElementById('cidr');
const ipError = document.getElementById('ip-error');
const cidrError = document.getElementById('cidr-error');
const networkAddress = document.getElementById('network-address');
const broadcastAddress = document.getElementById('broadcast-address');
const totalHosts = document.getElementById('total-hosts');
const usableHosts = document.getElementById('usable-hosts');
const subnetMask = document.getElementById('subnet-mask');
const wildcardMask = document.getElementById('wildcard-mask');
const ipClass = document.getElementById('ip-class');
const ipType = document.getElementById('ip-type');
ipInput.addEventListener('input', calculateSubnet);
cidrInput.addEventListener('input', calculateSubnet);
function calculateSubnet() {
const ip = ipInput.value.trim();
const cidr = cidrInput.value.trim().replace('/', '');
ipError.textContent = '';
cidrError.textContent = '';
if (!validateIP(ip)) {
ipError.textContent = 'Invalid IP Address';
clearResults();
return;
}
if (!validateCIDR(cidr)) {
cidrError.textContent = 'Invalid CIDR (must be between 0 and 32)';
clearResults();
return;
}
const ipParts = ip.split('.').map(Number);
const cidrValue = parseInt(cidr);
const subnetMaskValue = getSubnetMask(cidrValue);
const wildcardMaskValue = getWildcardMask(subnetMaskValue);
const networkAddressValue = getNetworkAddress(ipParts, subnetMaskValue);
const broadcastAddressValue = getBroadcastAddress(ipParts, subnetMaskValue);
const totalHostsValue = Math.pow(2, 32 - cidrValue);
const usableHostsValue = totalHostsValue - 2;
networkAddress.value = networkAddressValue.join('.');
broadcastAddress.value = broadcastAddressValue.join('.');
totalHosts.value = totalHostsValue;
usableHosts.value = usableHostsValue > 0 ? usableHostsValue : 0;
subnetMask.value = subnetMaskValue.join('.');
wildcardMask.value = wildcardMaskValue.join('.');
ipClass.value = getIPClass(ipParts);
ipType.value = isPrivateIP(ipParts) ? 'Private' : 'Public';
}
function validateIP(ip) {
const parts = ip.split('.');
if (parts.length !== 4) return false;
for (let part of parts) {
const num = parseInt(part);
if (isNaN(num) || num < 0 || num > 255) return false;
}
return true;
}
function validateCIDR(cidr) {
const num = parseInt(cidr);
return !isNaN(num) && num >= 0 && num <= 32;
}
function getSubnetMask(cidr) {
const mask = [0, 0, 0, 0];
let bits = cidr;
for (let i = 0; i < 4; i++) {
for (let j = 7; j >= 0; j--) {
if (bits > 0) {
mask[i] += (1 << j);
bits--;
}
}
}
return mask;
}
function getWildcardMask(subnetMask) {
return subnetMask.map(octet => 255 - octet);
}
function getNetworkAddress(ip, subnetMask) {
return ip.map((octet, index) => octet & subnetMask[index]);
}
function getBroadcastAddress(ip, subnetMask) {
return ip.map((octet, index) => octet | (~subnetMask[index] & 0xFF));
}
function getIPClass(ip) {
const firstOctet = ip[0];
if (firstOctet >= 1 && firstOctet <= 126) return 'A';
if (firstOctet >= 128 && firstOctet <= 191) return 'B';
if (firstOctet >= 192 && firstOctet <= 223) return 'C';
if (firstOctet >= 224 && firstOctet <= 239) return 'D';
if (firstOctet >= 240 && firstOctet <= 254) return 'E';
return 'Unknown';
}
function isPrivateIP(ip) {
const firstOctet = ip[0];
const secondOctet = ip[1];
if (firstOctet === 10) return true;
if (firstOctet === 172 && secondOctet >= 16 && secondOctet <= 31) return true;
if (firstOctet === 192 && secondOctet === 168) return true;
return false;
}
function clearResults() {
networkAddress.value = '';
broadcastAddress.value = '';
totalHosts.value = '';
usableHosts.value = '';
subnetMask.value = '';
wildcardMask.value = '';
ipClass.value = '';
ipType.value = '';
}
</script>
</body>
</html>
This subnet calculator is a single HTML file that includes all necessary HTML structure, CSS styling, and JavaScript logic. The application is designed with a clean, dark theme and is responsive. It performs subnet calculations in real-time as the user types, without