English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to be able to paste an unformated mac address example 000d1a2d3b4d into form field named mac, then when I change fields it automatically formats to a mac address. Example 00:0d:1a:2d:3b:4d after I change fields.
I paste 000d1a2d3b4d hit tab and it turns into 00:0d:1a:2d:3b:4d.

2006-07-10 08:03:42 · 2 answers · asked by j17349 1 in Computers & Internet Programming & Design

2 answers

Hey :)

1) in your HTML part (html-tag) you have to add:

onChange=" change(this.form.mac); "


2) and in your scripts part just add this function:

function change(field) {
var cnt = 0;
for (var i = 0; i < field.value.length; i++) {
var ch = field.value.charAt(i);
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) cnt++;
else if (ch == ':') cnt = 0;
else {
field.value = field.value.substring(0, i) + "*" + field.value.substring(i + 1); cnt++;
}
if (cnt > 2) {
field.value = field.value.substring(0, i) + ":" + field.value.substring(i); cnt = 0;
}
}
field.value = field.value.substring(0, 17);
}


then you can just paste your address and press to go to other fields and it will format the string automatically.

If there are any problems with the function - please, write me.

Sincerely,
Nikaustr

2006-07-14 21:15:05 · answer #1 · answered by nikaustr 2 · 0 0

function breakMAC(oldMAC){
newMAC="";
for(x=0;x<5;x++){
newMAC+=oldMAC.substr(x*2,2)+":";
}
newMAC+=oldMAC.substr(10,2);
return newMAC;
}

2006-07-10 15:18:56 · answer #2 · answered by John J 6 · 0 0

fedest.com, questions and answers