jQuery Validating Australian Phone Numbers

前端 未结 3 1577
日久生厌
日久生厌 2021-01-24 22:06

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

3条回答
  •  时光取名叫无心
    2021-01-24 22:26

    You can use the same regex/replace logic you have suggested.

    html

    Mobile:
    Landline:
    

    jquery

    $("#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);
    });
    

    Demo:https://jsfiddle.net/7c0d418t/

提交回复
热议问题