Follow code can be used to format the textbox which accepts only US Phone numbers. It assigns the hypens automatically after 3 and 3 numbers when the numbers are typed.
Output will be 123-456-7890
aspx:
< id="txtPhone" runat="server" onkeyup="ValidatePhoneUSFormat();" maxlength="12" onkeypress="return AllowOnlyNumbers(event);" onblur="ValidatePhoneUSFormat();">
javascript:
function ValidatePhoneUSFormat() {
var txtValue = document.getElementById('txtPhone').value;
var formatedValue = '';
for (var i = 0; i < i ="="" txtvalue =" txtValue" i ="="" txtvalue =" txtValue" value =" txtValue;" keychar =" e.keyCode"> 57) {
return false;
}
}
Known issues:
1. Code written only while typing the numbers, will update the code when the phone numbers are pasted from clipboard
2. Only numbers are allowed while typing, but allows alphabets while pasting, will update this as well.
No comments:
Post a Comment