It's a bit tricky to allow multiple formats depending on the values. Phone numbers have no real standard (except the starting numbers of 00 or + for international, 0 for national and 1-9 for provider/location at the beginning) and could also be of length 12 and they hardly depend on the country/provider/location of the target.
I suggest using two different input fields instead of two formations depending on the input length/value.
But something like this will do it:
<input type="text" id="user">
<script>
/*
* This allows:
@0123456789af, @01:23:45:67:89:af and 01:23:45:67:89:af
threaded as mac address
any other input like:
00:00:00:00:00 or 01:23:45:67:89:az
handled as phone number
(removes any non numeric value on focusout)
*/
IMask(user, {
mask: /[@:0-9a-f]/gi, // allow anything
prepare(char) {
// allow only @, :, 0-9 and a-f for input
// this allows: @[a-f0-9]{12} values to be entered or 00:00:00:00:00:00...
return char.replace(/[^@:0-9a-f]/i, '');
},
commit(value, masked) {
// leading @ will be removed
// check if it contains already a : and has a length of 6 sets with 2 byte length each
if (value.split(':').length == 6 && value.split(':').every(p => p.length == 2) && !value.startsWith('@')) {
// add an @ leading for the text below
value = '@' + value;
}
value = value.replace(/:/g, '');
const dummy = document.createElement("input");
// required else call for IMask() on dummy won't work
document.body.appendChild(dummy);
dummy.setAttribute("value", value.startsWith('@') ? value.substr(1) : value);
// length must be 12 and should not start with 00
if (/^@[0-9a-f]{12}$/i.test(value)) {
// remove the leading @ that was added before or by the user and add : formation
masked._value = IMask(dummy, {
mask: '**:**:**:**:**:**'
})._value;
} else {
// filter only numbers
masked._value = IMask(dummy, {
mask: /.*/,
prepare(str) {
return str.replace(/[^0-9]/, '');
}
})._value;
}
dummy.remove();
}
});
</script>