< script type = "text/javascript" >
//==================================
// xxx-xxx-xxxx Phnoe No Validation
//==================================
jQuery(document).ready(function() {
$("#phone").keydown(function(e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3) {
$("#phone").val(curval + "-");
} else if (curchr == 7) {
$("#phone").val(curval + "-");
}
});
}); < /script>In this code we learn how to validate phone no using jQuery.
