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 :)
I came up with 1 solution, not convinced how optimal it is, but someone may want to elaborate on it.
DEMO: https://jsfiddle.net/kb4u536a/
You can use the same regex/replace logic you have suggested.
html
jquery
Demo:https://jsfiddle.net/7c0d418t/
You can find here phone validation code for more than 200 countries:
https://github.com/googlei18n/libphonenumber