1

I have a web page that is supposed to take in a user's contact information and validate it, however my script doesn't seem to be functioning and I'm not sure why.

I know that you can get the input of a text field by using document.getElementById(name).value which is what I have, but my submit button doesn't respond with anything.

Any suggestions?

HTML:

<form>
    First Name:<br>
        <input type="text" id="first" name="firstname"><br>
    Last Name:<br>
        <input type="text" id="last" name="lastname"><br>
    Street Address:<br>
        <input type="text" id="addr" name="address"><br>
    City:<br>
        <input type="text" id="city" name="city"><br>
    State:<br>
        <input type="text" id="state" name="state"><br>
    Zip Code:<br>
        <input type="text" id="zip" name="zip"><br>
    Phone #:<br>
        <input type="text" id="phone" name="phone"><br>
    E-mail Address:<br>
        <input type="text" id="email" name="email"><br>
    <br>
    <button type="button" onclick="verify()">Submit</button>
</form>

JavaScript:

function verify() {
    var fname, lname, addr, city, state, zip, phone, email;
    var alpha = /[A-z]/i;

    fname = document.getElementById('first').value;
    lname = document.getElementById('last').value;
    addr = documnet.getElementById('addr').value;
    city = document.getElementById('city').value;
    state = document.getElementById('state').value;
    zip = document.getElementById('zip').value;
    phone = document.getElementById('phone').value;
    email = document.getElementById('email').value;

    var values = [fname, lname, addr, city, state, zip, phone, email];

    window.alert(fname);

    /*
    //Check to see if any fields are blank
    for(var i = 0; i < values.length; i++) {
        if(values[i] == "") {
            window.alert("Must have a value in each field");
            return false;
        }
    }

    //Check to see if text fields contain non-alphabetic characters
    if(!alpha.test(fname)) {
        window.alert("First name must contain only alphabetic characters");
        return false;
    }
    if(!alpha.test(lname)) {
        window.alert("Last name must contain only alphabetic characters");
        return false;
    }
    if(!alpha.test(city)) {
        window.alert("City must contain only alphabetic characters");
        return false;
    }
    if(!alpha.test(state)) {
        window.alert("State must contain only alphabetic characters");
        return false;
    }
    */
}
2
  • please check the browser console for errors.. Commented Jun 11, 2015 at 16:58
  • I just figured out how to do that, and found my error. Thanks! Commented Jun 11, 2015 at 17:01

1 Answer 1

1

You have a typo in

addr = documnet.getElementById('addr').value;

it should be

addr = document.getElementById('addr').value;

notice the spelling of document

Other than that, your code seems to be working fine..see the fiddle

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

1 Comment

I just caught that. Thanks!

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.