3
Hello!
I’m trying to alter a mask from a field of my form. Below in the field highlighted in blue, is as it is currently, and in the second field is as wish it remains (without the space between the )
and the 1
). I wish to change this because by allowing the data to be sent in the first way, I have some problems when returning results from the database.
Below the Javascript code that is making the mask.
I tried to remove the space after )
on the line texto = [texto.slice(0, 3), ") "
but I had problems with the operation of the field.
$("#txtTelefone2").bind('input propertychange', function () {
var texto = $(this).val();
texto = texto.replace(/[^\d]/g, '');
if (texto.length > 0) {
texto = "(" + texto;
if (texto.length > 3) {
texto = [texto.slice(0, 3), ") ", texto.slice(3)].join('');
}
if (texto.length > 12) {
if (texto.length > 13)
texto = [texto.slice(0, 10), "-", texto.slice(10)].join('');
else
texto = [texto.slice(0, 9), "-", texto.slice(9)].join('');
}
if (texto.length > 15)
texto = texto.substr(0, 15);
}
$(this).val(texto);
})
How can I fix this? Thank you!
Sergio, thank you for the answer, actually works for the proposed problem, but the hyphen shift is not working as it was originally. + 1 for the good explanation.
– Victor Alencar Santos
@Victoralencarsantos, Hmmm hifen displacement was not requested in the question. Can you explain what different cases you want for the hyphenation position and for what length? So I can at least leave the right answer.
– Sergio
Hi @Sergio. Originally, the hyphen was positioned like this: (xx) xxxx-xxxx if they were 8-digit numbers, and so (xx) xxxxx-xxxx if they were 9-digit numbers.
– Victor Alencar Santos
@Victoralencarsantos, thanks. I improved the answer to if anyone else needs.
– Sergio
Hello @Sergio! The form of displacement is correct, but using your Fiddle in this example are being inserted 6 numbers before the hyphen (11)111111-1111. See here as it was originally, if you want to make any changes. Thank you!
– Victor Alencar Santos
@Victoralencarsantos, okay, is that how it should work? : http://jsfiddle.net/HP2Wx/2/
– Sergio
Perfect, @Sergio!
– Victor Alencar Santos