Change tab of the fields of a MacAddress

2

I would like to implement an event that would correct data that would come out of the standard, replacing the separator with : and putting the letters in lowercase. For example, the user types 00-25-66-F0-21-11 , would be corrected to 00:25:66:f0:21:11 .

My current code:

<html>

<form name="muda">MAC: <input type="text" name="mac" id="macad" onmouseover="capturamac()" /></form>

<script>
function capturamac(){
    var macadd = toString(muda.mac.value);
    var corrige = macadd.split('-').join(':').toLowerCase();
    document.getElementById('macad').value = toString.corrige;
}
</script>

</html>
    
asked by anonymous 04.12.2018 / 18:16

2 answers

2

An alternative is to use split to break the string in an array, and then use join to generate a string again.

The parameter of split is the separator (in this case, - ). This will generate an array with the parts of the address ( 00 , 25 , etc). And the join parameter is the new separator (in this case, : ), so the parts of the array will be joined into a single string, with the parts separated by : . Finally, I used toLowerCase() to make the letters lowercase:

let endereco = '00-25-66-F0-21-11';

let correto = endereco.split('-').join(':').toLowerCase();

console.log(correto); // 00:25:66:f0:21:11

As you are only exchanging a single character for another, I find this solution easier. But the @Storack solution with regex is also valid, and I'd use it for more complex cases (when the is not just a single character, or it can be "any tiny character or digits", or any other criteria that is easier to do with regex).

As for the Object undefined error, it occurs because of the way you are trying to capture the value of input and then set it back.

For example, calling toString is unnecessary, since the value is already a string . E onmouseover calls the conversion function when the mouse is placed on the field. The right thing would be to convert the value only after the focus is taken from the field, and for this onblur .

The corrected code looks like this:

function capturamac(){
    var macadd = muda.mac.value;
    var corrige = macadd.split('-').join(':').toLowerCase();
    muda.mac.value = corrige;
}
<html>

<form name="muda">MAC: <input type="text" name="mac" id="macad" onblur="capturamac()" /></form>

</html>
    
04.12.2018 / 18:29
1

First use toLowerCase in its string :

  

toLowerCase

     

The toLowerCase () method returns the string called converted to lowercase.

Then use replace :

  

replace

 str.replace(regexp|substr, newSubStr|function)
     

The replace () method returns a new string with some or all combinations of the pattern replaced with a substitute. The default can be a string or a RegExp, and the substitute can be a string or a function to be called by each combination.

Note that replace can receive string as the second parameter, but if this type of data is provided, only the first occurrence will be replaced. So I recommend using a regular expression:

/-/g

Where the literal character - will be matched and the g (global) modifier will be used.

Applying this to a conversion function:

const converter = entrada => entrada.toLowerCase().replace(/-/g, ':');

console.log(converter('00-25-66-F0-21-11'));

The expression above looks for the literal character - and uses the g (global) modifier.

    
04.12.2018 / 18:24