Javascript 验证澳大利亚电话号码

Javascript 验证澳大利亚电话号码,javascript,regex,Javascript,Regex,我一直在尝试使用javascript验证一个澳大利亚电话号码,但它接受了一切。它需要10个数字长,以0开始接受空格: 0243452334 并且一起 0243452334 我认为正则表达式可能是错误的,或者代码本身是错误的 function PhoneNumberVal(form){ var phoneFormat= /^0[0-8]{2})\)?[ ]?([0-9]{4})[ ]?([0-9]{4})$/; var phoneLength = document.getEle

我一直在尝试使用javascript验证一个澳大利亚电话号码,但它接受了一切。它需要10个数字长,以0开始接受空格:
0243452334

并且一起
0243452334

我认为正则表达式可能是错误的,或者代码本身是错误的

function PhoneNumberVal(form){
    var phoneFormat= /^0[0-8]{2})\)?[ ]?([0-9]{4})[ ]?([0-9]{4})$/;  
    var phoneLength = document.getElementById('phone').value.length;

    if(phoneFormat.test(phoneLength)) {
        return true;
    } else {
        alert("Not a valid phone number");
        return false;
    }
}

你的正则表达式错了<代码>^0[0-8]{2}\)?[]?([0-9]{4})[]?([0-9]{4})$您未能放入左括号,您需要将
[0-8]{2}
更改为
[0-8]
,因为您的输入正好包含10位数字

^(?:\(0[0-8]\)|0[0-8])[ ]?[0-9]{4}[ ]?[0-9]{4}$
正则表达式?哈!

更新:此版本应为最终版本

只要这样做:

function IsAustralianTelephoneNumberValid(a_telephone)
{
    a_telephone = a_telephone.replace(/\s/g, ''); // remove all spaces

    // if is empty OR first char is NOT 0 
    if((a_telephone=='')||(a_telephone.charAt(0)!='0'))
    {
        alert("Not a valid phone number");
        return false;
    }

    // lets save the length of that string before we remove digits
    length_with_digits = a_telephone.length;

    // now string has its digits removed
    a_telephone = a_telephone.replace(/0|1|2|3|4|5|6|7|8|9/g,'');

    // if is nothing, then there was no other characters in string
    // except digits and spaces AND ALSO if the difference of length before the digits
    // removal and now is 10 then we can be sure we had 10 digits and nothing else,
    // so its valid. Any other case is not valid.
    if((a_telephone=='')&&(length_with_digits-a_telephone.length==10))
    {
        alert('ok');
        return true;
    }
    else
    {
        alert("Not a valid phone number");
        return false;
    }
}
使用此正则表达式

/^\D*0(\D*\d){9}\D*$/

提示:不要用空格。插入后修剪值并删除空格,然后继续匹配所需的值。请尝试以下正则表达式:-
/^0[0-8]\d{8}$/g
^0[0-8]\?[]?([0-9]{4})[]?([0-9]{4})$
它还需要从0@programmingHelp哦,那很容易。我会解决这个问题给我一点时间。它不会验证空格。看起来好像有超过10个数字