I am trying to get the value from an input box, validate & format the number, then update the input field.
I want it to validate all Australian phone numbers (mobile and landline) formatting mobile numbers to 04XX XXX XXX and Landline numbers to (0X) XXXX XXXX
var phone_number = $("#phone").val();
//validate mobile number
var formatted = phone_number.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
//replace number
$("#phone").val(formatted);
Any help would be awesome :)
You can use the same regex/replace logic you have suggested.
Mobile:<input id = "mobile" type = "tel" maxlength=8></input>
Landline:<input id = "landline" type = "tel" maxlength=10></input>
$("#mobile").blur(function(){
var mobile_ele = $("#mobile");
var mobileNum = mobile_ele.val();
var formattedNum = mobileNum.replace(/(\d{2})(\d{3})(\d{3})/g,"04$1 $2 $3");
mobile_ele.val(formattedNum);
});
$("#landline").blur(function(){
var landline_ele = $("#landline");
var landlineNum = mobile_ele.val();
var formattedNum = landlineNum.replace(/(\d{2})(\d{4})(\d{4})/g,"($1) $2 $3");
mobile_ele.val(formattedNum);
});