jQuery 验证澳大利亚电话号码

问题描述 投票:0回答:4

我正在尝试从输入框中获取值,验证数字并设置其格式,然后更新输入字段。

我希望它验证所有澳大利亚电话号码(手机和固定电话) 将手机号码格式化为 04XX XXX XXX 和固定电话号码 (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);

任何帮助都会很棒:)

javascript jquery regex validation formatting
4个回答
3
投票

您可以使用您建议的相同正则表达式/替换逻辑。

html

Mobile:<input id = "mobile" type = "tel" maxlength=8></input>
Landline:<input id = "landline" type = "tel" maxlength=10></input>

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);
});

演示:https://jsfiddle.net/7c0d418t/


3
投票

我想出了 1 个解决方案,但不确信它有多优化,但有人可能想详细说明它。

    function validatePhoneNumber(phone_number){
        var formatted = "";
        //remove all non-digits
        phone_number = phone_number.replace(/\D/g,'');
        //if number starts with 61, replace 61 with 0
        if (phone_number.match(/^61/)){
              phone_number = "0"+phone_number.slice(2);
        }

        if (phone_number.match(/^04/)){
            if (phone_number.length === 10){
                var formatted = phone_number.replace(/(\d{4})(\d{3})(\d{3})/g,"$1 $2 $3");
            } else {
                alert('Invalid phone number');
            }
        } else if (phone_number.match(/^02|03|07|08/)){
            if (phone_number.length === 10) {
                var formatted = phone_number.replace(/(\d{2})(\d{4})(\d{4})/g,"($1) $2 $3");
            } else {
                alert('Invalid phone number');
            }
        } else if (phone_number.length === 8){
            alert('Please use Area Code for landline numbers');
        } else {

            alert('Invalid phone number');
        }
        //update
        $("#phone").val(formatted);
    }

演示:https://jsfiddle.net/kb4u536a/


1
投票

您可以在这里找到 200 多个国家/地区的电话验证码:

https://github.com/googlei18n/libphonenumber


0
投票
jQuery.validator.addMethod("weekText",function(a,b){
    
    //return a=a.replace(/\s+/g,""),this.optional(b)||a.length>9&&a.match(/^(\+?61-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/)
    //return a=a.replace(/\s+/g,""),this.optional(b)||a.match(/^(\+?61-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/);
    let result = a.match(/^(\+?61-?)/);
    let result1 = a.match(/^(\+?4-?)/);
    if(result == "+61,+61")
    {
        return true;
        //alert("dsdsfsf");
        //return a=a.match(/^(\+?61-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/);
    }
    else if(result == "+4,+4")
    {
        return true;
        //alert("Here1");
        //return a=a.match(/^(\+?04-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/);
    }
    else
    {
        return false;
    }
    
},"Please specify a valid phone number");

//Validation for Footer form
var wh = jQuery("#footerform").validate({
    rules: {
    'fullname':{
    required:true
    },
    
    'footer-email':{
    required: true,
    email:true
    },
    'footer-phone':{
    required:true,
    //phoneAu: true,
    //rangelength : [5, 16],
    weekText: true
    },
    'footer-address':{
    required:true,
    lettersonly: true
    },
    'footer-message':{
    lettersonly: true
    }
  },
  messages: {
    'fullname': {
    required: "Please enter your name."
    },
    
    'footer-email': {
    required: "Please enter your email.",
    email: "Please enter valid email address."
    },
    'footer-phone' : {
    required: "Please enter your phone.",
    //rangelength : "Please enter valid phone number.",
     weekText : "Please specify a valid phone number"
    },
    'footer-address': {
    required: "Please enter your address.",
    lettersonly: "Please enter Letter Only.",
    },
    'footer-message':{
    lettersonly: "Please enter Letter Only.",
    }
  },
  errorPlacement: function(error, element)
  {
  error.insertAfter( element );
  },
  errorElement: "label",
  errorClass: "error"
});
jQuery('#footerform .submit_btn_box').on('click', function(event) {
if (wh.form())
{
/*var response = jQuery('#g-recaptcha-response').val();
  if(response.length == 0  )
  {
    jQuery('#contactform .recaptcha-error-message').show();
    return false;
  }
  else
  {
    jQuery('#contactform .recaptcha-error-message').hide();
  } */          
}
else
{
  /*var response = jQuery('#g-recaptcha-response').val();
  if(response.length==0  )
  {
  jQuery('#contactform .recaptcha-error-message').show();
  return false;
  }
  else
  {
  jQuery('#contactform .recaptcha-error-message').hide();
  }*/
  event.preventDefault();
  event.stopPropagation();
}
© www.soinside.com 2019 - 2024. All rights reserved.