• 0

MAC Address Format Changer


Question

I am building a web form to track some data as I am working on it, and one of the pieces of information that is needed in the for is a MAC address. I have to use the MAC address in both the standard AA:BB:CC:DD:EE:FF format and the Cisco AABB.CCDD.EEFF format. I am wanting to add a button on the web form, in JavaScript preferably, that will switch it from form to the other.

Example:

I paste the MAC of AA:BB:CC:DD:EE:FF in the field, I click the button it will switch it to AABB.CCDD.EEFF, and if I hit the button again it will switch it back to AA:BB:CC:DD:EE:FF.

Any help would be appreciated.

Thanks

ltworf

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Have you found any solution? If not, I have made something very fast. :unsure:

<html>
	<head>
		<title>MAC address converter</title>
	</head>
	<script type="text/javascript">
		function fnValidateMacAddress(macaddress) {
   			var mac1 = /^[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}$/;
			var mac2 = /^[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}\.[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}\.[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}$/;

			var macaddr = macaddress.value
			if (mac1.test(macaddr)) {
				var macaddr = macaddr.replace(/:/g, "");
				macaddress.value = fnConvertMacAddress(macaddr, ".", 4);
   			}else if (mac2.test(macaddr)) {
					  var macaddr = macaddr.replace(/\./g, "");
				macaddress.value = fnConvertMacAddress(macaddr, ":", 2);
   			} else {
   				alert("Invalid MAC address");
   			}
		}

		function fnConvertMacAddress(string,chr,nth){
			var output = '';
			  for (var i=0; i<string.length; i++) {
				if (i>0 && i%nth == 0)
					  output += chr;
					output += string.charAt(i);
			  }

			  return output;
		}

	</script>
	<body>
		<form>
			<input type="text" value="" name="macaddress"/>
			<input type="button" value="convert" onClick="fnValidateMacAddress(macaddress)"/>
		</form>
	</body>
</html>

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.