I need help with a text format script.
HTML CODE:
<label for="primary_phone">Primary Phone Number<span class="star">*</span></label>
<br>
<input type="text" name="primary_phone" id="primary_phone" class="_phone required-input" value="" maxlength="10">
CSS CODE:
.invalid{
border:1px solid red !important;
}
.valid{
border:1px solid green !important;
}
JS CODE:
function phoneFormat(){
$( "._phone" ).on('blur change', function() {
text = $(this).val().replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
var testt=$(this).val().match(text);
if($(this).val()=='' || $(this).val().match(text) || $(this).val().length == 0)
{
$(this).removeClass('valid').addClass('invalid');
}
else{
$(this).removeClass('invalid').addClass('valid');
}
$(this).val(text);
});
}
$( "#primary_phone" ).blur(function() {
phoneFormat();
});
The script formats the text like this:
1234567890
Which appears as:
(123) 456-7890
The issue arises when trying to edit the phone number, due to maxlength="10"
I want the user to be able to input up to 10 characters. How can I achieve both requirements?
If anything is unclear, I will provide more details in an update. Thank you!