javascript正则表达式地址编号

时间:2012-04-18 01:08:53

标签: javascript regex

我正在创建一个网站,并使用javascript来验证信息 这是我的address_number有问题的代码 错误是无效的地址编号

如果有任何其他错误,请说

thanks =)

function validateForm()
{
    var form = document.forms['inputForm'];
    var formats = 
        {
            first_name: /^[a-zA-Z]+[\-'\s]?[a-zA-Z]+$/,                             /*works for a-Z allows - and '*/
            surname: /^[a-zA-Z]+[\-'\s]?[a-zA-z]+$/,                                /*works for a-Z allows - and '*/
            postcode: /^\d{4}$/,                                                    /*4digit post code australia wide*/
            email: /^\w+([\.-]w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/,                   /*allows all word characters and normal email formats*/
            address_number: /^\d[0-9]{\?12}$/,                                                  /*allows any number of digits*/
            address_name: /^\w?\s?[a-zA-Z]+(,?\s([a-zA-Z])*)*$/,            /*allows numbers space capital letters and other word characters*/
            suburb: /^\w?\s?[a-zA-Z]+(,?\s([a-zA-Z])*)*$/,          /*allows numbers space capital letters and other word characters*/
            phone: /^\d{10}$/,                                                      /*8 number phone number*/
            length: /^\d[0-9]$/,
            height: /^\d[0-9]$/,
        }
        var length = form.length.value;
        var height = form.height.value;
        var area = length*height;

        var elCount = form.elements.length;
        for(var i = 0; i<elCount; i++)
        {
            var field = form.elements[i];
            if(field.type == 'text')
            {
                if(!formats[field.name].test(field.value))
                {
                    alert('invalid '+ field.name.replace('_',' ')+'.');             /*alerts the name of the area not filled right in a pop up box*/
                    field.focus();
                    return false;
                }
            }
        }
        alert('All fields correct, the form will now submit.')
}

1 个答案:

答案 0 :(得分:0)

\d+应该是任何数字,至少一次,但就像你想要的那样多。