pepsicode
pepsicode

Reputation: 695

data validation javascript

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>

Upvotes: 0

Views: 683

Answers (3)

muffel
muffel

Reputation: 7390

just attach to the onclick event and it works:

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

Upvotes: 0

RobG
RobG

Reputation: 147503

In this test:

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

you left out a return.

Upvotes: 1

Nabor
Nabor

Reputation: 1701

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.

Upvotes: 0

Related Questions