0

im creating a website and im using javascript to validate information this is the code im having trouble with the address_number the error is invalid address number

if there are any other errors please say

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.')
}
minixe
  • 5
  • 1
  • 3
  • 1
    We need much more information - like the input! – John Fisher Apr 18 '12 at 01:11
  • by input u mean what im typing in to test? if so then first name mat surname ireland postcode 2145 email myemail@hotmail.com address number 52 address name smith street suburb sydney phone 1112345678 length and height is to calculate the area of a rectangle the alert is to say what field isn't filled in correct if this isn't the information you need please let me know – minixe Apr 18 '12 at 01:23
  • Why bother with all that? Just let users put in what they want. Your regular expression for name fields only allows one hyphen or space, `address_number` doesn't allow things like 2/34. The `address_name` doesn't allow numbers (e.g. 23rd st) or commas if there is more than just a street name, same for suburb, and phone numbers can be fewer than 10 characters (even though the comment says 8), and so on. – RobG Apr 18 '12 at 03:06

1 Answers1

0

\d+ should be any digit, at least once, but like, however many as you'd like.

tkone
  • 22,092
  • 5
  • 54
  • 78