Changing fields separator for a Macaddress

Asked

Viewed 57 times

2

I would like to implement an event that fixes data that is outside the standard, replacing the tab with : and put the letters in lower case. For example, the user type 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>
  • and what’s wrong with your code? It’s not right??

  • Is giving [Object Undefined]

  • 1

    The toString is in the wrong place. Would: muda.mac.value.toString(). Although you don’t even need to convert to string, since the value already seems to be a string. But the Sorack answer already said it all :D

2 answers

2

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

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

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

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

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


Since you are only changing one character for another, I find this solution easier. But the solution from @Sorack with regex is also valid, and I would use it for more complex cases (when the separator is not just a single character, or it can be "any tiny character or digits", or any other criterion that is easier to do with regex).


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

For example, call toString is unnecessary because the value is already a string. And onmouseover calls the conversion function when the mouse is placed over the field. The right would be to convert the value only after the focus is removed from the field, and for that use 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>

  • I’m not getting "print" back the corrected value. It’s giving [Object Undefined]. Is an input field.

  • @Andersonfidelis So there is some problem elsewhere (and not in the conversion itself). I suggest you edit your question by putting exactly the code that generates the error (and on which line the error occurs).

  • That’s the problem! no error! I’ve converted to String, but I can’t find a way to fix it

  • If you’re giving Object Undefined is because you’re referencing something that doesn’t exist. Or it’s muda, or muda.mac, or document.getElementById('mac'), or any other. One of them doesn’t exist. Check the console (F12 in Chrome and Firefox) if these elements exist on your page

  • Because it is @hkotsubo, my implementation is in iframe. When I ask to check the source code it does not show the id.

  • Maybe that’s it then, to get elements inside an iframe is a little more boring: https://stackoverflow.com/q/1088544

  • I brought my code to another html file to see if it was the same error of iframe. I found that no.

  • @Andersonfidelis I updated the answer, see if now solves

Show 3 more comments

1

First use toLowerCase in his 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 by 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 the replace may receive a string as a second parameter, but if this type of data is provided, only the first occurrence will be replaced. Therefore I recommend the use of a regular expression:

/-/g

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

Applying this in a conversion function:

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

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

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

  • Thanks Sorack, I just don’t understand the console log.

  • @Andersonfidelis just to show the same information, you can substitute for the assignment in the field

  • Perfect, in this case I put console.log(converter(muda.mac.value)) and gave [Object Undefined]

  • @Andersonfidelis Qual o id country?

  • @Andersonfidelis The problem must be in muda or in the muda.mac. Try to do console.log(muda) and console.log(muda.mac) to see what’s in these variables

  • "muda" is my form! "mac" is the name of the input field!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.