0

I have some problems on my form (validation.) They don't give any error when the datas are not given. How can I fix this problem? This is just some part of my code. For letter, numbers and e-mail types, it works perfectly. Thank you for your help.

function onFormSubmit(form_element){
    var checked = 0;
    if (form_element.gender[0].checked == false && form_element.gender[1].checked == false){
        alert("Please choose your Gender:Male of Female");
        return false;
    }

    if (form_element.make.selectedIndex == 0){
        alert("Please select your rating interval.");
        form_element.make.focus();
        return false;
    }

    if (form_element.click.checked == false){
        alert("Please confirm your registration!");
        false;
    }

    return true;
}

html

<form class="contact_form" action="" method="post" name="contact_form" onsubmit="return onFormSubmit(this)">
    <ul>
        <li>
            <h2>Sign-Up Form</h2>
        </li>
        <li>
            <label for="Male">Male:</label>
            <input type="radio" name="gender" value="m" /> &nbsp; Female:<input type="radio" name="gender" value="f" />
            <br />
        </li>
        <li>
            <label for="National Rating">National Rating:</label>
            <select name="make">
                  <option selected>-- SELECT --</option>
                  <option> Below 1200 </option>
                  <option> 1200 - 1500 </option>
                  <option> 1500 - 1800 </option>
                  <option> 1800 - 2100 </option>
                  <option> Above 2100 </option>
            </select><br />
        </li>
        <li>
            <input type="checkbox" name="click" value="regist"> I confirm my registration.
            <br />
        </li>
        <li>
            <button class="submit" type="submit">Submit</button>
        </li>
    </ul>
</form>
<div id="error_message" style="color:#ff0000;"></div>
3
  • I tire of saying this to people, but please indent your code properly so it's readable to other human beings. Your code is currently a right mess and it's hardly possible to follow where one scope ends and another begins Commented Apr 23, 2012 at 6:36
  • edited indentations, you're welcome. but i see something wrong with the code itself Commented Apr 23, 2012 at 6:38
  • How can I indent the code properly? Could you please help? Commented Apr 23, 2012 at 6:44

3 Answers 3

1

In this test:

> if (form_element.click.checked == false){
>           alert("Please confirm your registration!");
>           false; 

you left out a return.

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

1 Comment

I tried them with return and without return several times. It doesn't work. Moreover, they say my code is like mess. Which part is like mess? HTML or javascript? And how can I write it properly? I appreciate your help.
0

From your functions name I would say you call it during onSubmit? Most Browsers don't show alerts during onSubmit. I suggest to first do validation and then submit the data.

1 Comment

Where did you get that idea? All browsers I know of allow alerts to be shown from a submit listener.
0

just attach to the onclick event and it works:

<form class="contact_form" action="" method="post" name="contact_form" onclick="onFormSubmit">

1 Comment

instead of onsubmit="return onFormSubmit(this) ?

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.