Reputation: 3
I'm using this function to phone mask and works almost perfectly.
function mask(o, f)
{
v_obj = o;
v_fun = f;
setTimeout("execmask()", 1)
};
function execmask()
{
v_obj.value = v_fun(v_obj.value)
};
function mphone(v){
v=v.replace(/\D/g,"");
v=v.substring(0, 11);
v=v.replace(/^(\d{2})(\d)/g,"(OXX$1) $2");
v=v.replace(/(\d)(\d{4})$/,"$1-$2");
return v;
}
Here I run the mask in the text field:
<input type="text" id="phone" name="phone" onkeypress="mask(this, mphone);" onblur="mask(this, mphone);" />
The problem is that I need to change this part of the code (OXX$1)
to (0XX$1)
.
Current situation:
No. Of Digits | Input Field |
---|---|
9 digit | (OXX99) 99999-9999 |
8 digit | (OXX99) 9999-9999 |
The correct formatting that I need:
No. Of Digits | Input Field |
---|---|
9 digit | (0XX99) 99999-9999 |
8 digit | (0XX99) 9999-9999 |
The amount of 8 or 9 digits is the choice of the user.
Changing O
to 0
, causes an error in the mask.
Upvotes: 0
Views: 38080
Reputation: 323
In this project you can see the use of several methods below, including the phone mask and applying them to an input:
Repository: react-text-field-mask
Demo sandbox: https://codesandbox.io/s/eager-monad-qzod32
Regex masks:
// (00) 00000-0000
const phoneMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{5})(\d{4})/, '$1-$2');
};
// (00) 0000-0000
const landlineTelephoneMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d{4})/, '$1-$2');
};
// 10.000,00 -> 10000.00
const removeMoneyMask = (value: string | number) => {
const originalNumber = Number(value);
const numberIsWithMask = Number.isNaN(originalNumber);
if (numberIsWithMask) {
const stringValue = value.toString();
const splitedMoneyMask = stringValue
.split('.')
.join('')
.split(',')
.join('.');
return splitedMoneyMask.replace(/[^0-9.-]+/g, '');
}
return value.toString();
};
type BehaviorMode = 'standard' | 'typing';
const moneyMaskCustomization = {
/**
* typingMode - The value is typed from right to left:
* - '1' -> '0.01'
* - '10' -> '0.10'
*
* defaultMode - Simple conversion:
* - '1' -> '1.00'
* - '10' -> '10.00'
*/
maskBehaviorMode: (behaviorMode: BehaviorMode, value: string | number) => {
const numberWithoutMask = removeMoneyMask(value);
const normalizedMoneyValue = moneyMaskCustomization.normalizeMoneyValue(
numberWithoutMask.toString(),
);
const integerWithoutDecimalPlaces = normalizedMoneyValue.length === 1;
if (behaviorMode === 'typing' && integerWithoutDecimalPlaces) {
const newNumberFormat = Number(normalizedMoneyValue) / 100;
return Number(newNumberFormat).toFixed(2);
}
if (behaviorMode === 'standard' && integerWithoutDecimalPlaces) {
return Number(normalizedMoneyValue).toFixed(2);
}
return normalizedMoneyValue;
},
normalizeMoneyValue: (numberToNormalized: string) => {
const [stringInteger, stringDecimal] = numberToNormalized.split('.');
if (stringDecimal && stringDecimal.length === 1) {
const lastPositionOfTheInteger = stringInteger.length - 1;
const lastToIntegerPlace = stringInteger[lastPositionOfTheInteger];
if (lastPositionOfTheInteger !== 0) {
const firstIntegerPlace = stringInteger.substring(
0,
lastPositionOfTheInteger,
);
return `${firstIntegerPlace}.${lastToIntegerPlace}${stringDecimal}`;
}
return `${0}.${lastToIntegerPlace}${stringDecimal}`;
}
if (stringDecimal && stringDecimal.length === 3) {
const firstDecimalPlace = stringDecimal.substring(0, 1);
const lastTwoDecimalPlaces = stringDecimal.substring(1, 3);
const integerIsZero = Number(stringInteger) === 0;
if (integerIsZero) {
return `${firstDecimalPlace}.${lastTwoDecimalPlaces}`;
}
return `${stringInteger}${firstDecimalPlace}.${lastTwoDecimalPlaces}`;
}
return numberToNormalized;
},
};
// 10.000,00
const moneyMask = (
value: string | number,
maskBehaviorMode: BehaviorMode = 'standard',
) => {
if (value || Number.isInteger(value)) {
const moneyValue = moneyMaskCustomization.maskBehaviorMode(
maskBehaviorMode,
value,
);
return moneyValue
.replace(/\D/g, '')
.replace(/\D/g, '.')
.replace(/(\d)(\d{2})$/, '$1,$2')
.replace(/(?=(\d{3})+(\D))\B/g, '.');
}
return '';
};
// 000.000.000-00
const cpfMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue
.replace(/\D/g, '')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d{1,2})/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1');
};
// 00.000.000/0000-000
const cnpjMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1/$2')
.replace(/(\d{4})(\d{2})/, '$1-$2');
};
// 000.000.000-00 or 00.000.000/0000-000
const cpfOrCnpjMask = (value: string | number) => {
const stringValue = value.toString();
if (stringValue.length >= 15) {
return cnpjMask(value);
}
return cpfMask(value);
};
// 00000-000
const cepMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue.replace(/\D/g, '').replace(/^(\d{5})(\d{3})+?$/, '$1-$2');
};
// 00/00/0000
const dateMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{4})(\d)/, '$1');
};
const onlyLettersMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue.replace(/[0-9!@#¨$%^&*)(+=._-]+/g, '');
};
const onlyNumbersMask = (value: string | number) => {
const stringValue = value.toString();
return stringValue.replace(/\D/g, '');
};
Upvotes: 1
Reputation: 71
I have made some changes:
function mascaraFone(event) {
var valor = document.getElementById("telefone").attributes[0].ownerElement['value'];
var retorno = valor.replace(/\D/g, "");
retorno = retorno.replace(/^0/, "");
if (retorno.length > 10) {
retorno = retorno.replace(/^(\d\d)(\d{5})(\d{4}).*/, "($1) $2-$3");
} else if (retorno.length > 5) {
if (retorno.length == 6 && event.code == "Backspace") {
// necessário pois senão o "-" fica sempre voltando ao dar backspace
return;
}
retorno = retorno.replace(/^(\d\d)(\d{4})(\d{0,4}).*/, "($1) $2-$3");
} else if (retorno.length > 2) {
retorno = retorno.replace(/^(\d\d)(\d{0,5})/, "($1) $2");
} else {
if (retorno.length != 0) {
retorno = retorno.replace(/^(\d*)/, "($1");
}
}
document.getElementById("telefone").attributes[0].ownerElement['value'] = retorno;
}
<input id="telefone" onkeyup="mascaraFone(event)" />
Upvotes: 0
Reputation: 703
I love this function and I use it all the time. I've added 2 other masks if anyone needs them. I understand that they don't directly answer the question, but they are super useful.
//Social Security Number for USA
function mssn(v) {
var r = v.replace(/\D/g,"");
r = r.replace(/^0/,"");
if (r.length > 9) {
r = r.replace(/^(\d\d\d)(\d{2})(\d{0,4}).*/,"$1-$2-$3");
return r;
}
else if (r.length > 4) {
r = r.replace(/^(\d\d\d)(\d{2})(\d{0,4}).*/,"$1-$2-$3");
}
else if (r.length > 2) {
r = r.replace(/^(\d\d\d)(\d{0,3})/,"$1-$2");
}
else {
r = r.replace(/^(\d*)/, "$1");
}
return r;
}
//USA date
function mdate(v) {
var r = v.replace(/\D/g,"");
if (r.length > 4) {
r = r.replace(/^(\d\d)(\d{2})(\d{0,4}).*/,"$1/$2/$3");
}
else if (r.length > 2) {
r = r.replace(/^(\d\d)(\d{0,2})/,"$1/$2");
}
else if (r.length > 0){
if (r > 12) {
r = "";
}
}
return r;
}
Upvotes: -5
Reputation: 89171
function mask(o, f) {
setTimeout(function () {
var v = f(o.value);
if (v != o.value) {
o.value = v;
}
}, 1);
}
function mphone(v) {
var r = v.replace(/\D/g,"");
r = r.replace(/^0/,"");
if (r.length > 10) {
// 11+ digits. Format as 5+4.
r = r.replace(/^(\d\d)(\d{5})(\d{4}).*/,"(0XX$1) $2-$3");
}
else if (r.length > 5) {
// 6..10 digits. Format as 4+4
r = r.replace(/^(\d\d)(\d{4})(\d{0,4}).*/,"(0XX$1) $2-$3");
}
else if (r.length > 2) {
// 3..5 digits. Add (0XX..)
r = r.replace(/^(\d\d)(\d{0,5})/,"(0XX$1) $2");
}
else {
// 0..2 digits. Just add (0XX
r = r.replace(/^(\d*)/, "(0XX$1");
}
return r;
}
Upvotes: 12