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.')
}
3
  • 1
    We need much more information - like the input! Commented Apr 18, 2012 at 1:11
  • by input u mean what im typing in to test? if so then first name mat surname ireland postcode 2145 email [email protected] 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 Commented Apr 18, 2012 at 1: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. Commented Apr 18, 2012 at 3:06

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

And \d[0-9] is redundant, just \d will suffice.
@Lazerblade Yup. That's why I left it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.