//Code Starts
function validatePhone(txtPhone) {
var a = document.getElementById(txtPhone).value;
var filter = /^[0-9-+]+$/;
if (filter.test(a)) {
return true;
}
else {
return false;
}
}
//Code Ends
The above regular expression allows only numerals and + or – signs. Based on your requirement you can modify this regular expression.Now call this function to validate the phone number. For demo, I have used on blur event of textbox But It can be used on click on button or any another event. Based on the result, it updates the status in a span tag.
//Code Starts
$('#txtPhone').blur(function(e) {
if (validatePhone('txtPhone')) {
$('#spnPhoneStatus').html('Valid');
$('#spnPhoneStatus').css('color', 'green');
}
else {
$('#spnPhoneStatus').html('Invalid');
$('#spnPhoneStatus').css('color', 'red');
}
});
//Code Ends
See result below. Feel free to contact me for any help related to jQuery, I will gladly help you.