Hi, below is the code to validate the US phone number. In my previous post, I've posted similar code, but here the format is different.
Format: (123)456-7890
I've used jQuery to do this. You have to download the jQuery library code from www.jquery.com and place the script source above the javascript code you use.
aspx:
'< id="txtPhoneNumber" runat="server" maxlength="13">
javascript:
$("#txtPhoneNumber").live("keypress", function(e) {
var keyChar = e.keyCode ? e.keyCode : e.charCode;
if (keyChar <> 57) {
return false;
}
});
$("#txtPhoneNumber").live("keyup", function(e) {
var txtValue = $(this).val();
if ((txtValue.length - 1) == 0) {
txtValue = '(' + txtValue;
}
if (($(this).val().length == 4) && ($(this).val().charAt(3) != ')')) {
txtValue = txtValue + ')';
}
if ($(this).val().length == 8) {
txtValue = txtValue + '-';
}
$(this).val(txtValue);
});
* Validation is done only while typing the phone number
* Only numbers are allowed to type
No comments:
Post a Comment