Red Vulpine
Red Vulpine

Reputation: 453

Validate checkbox with JS

I'm searching a while for this and i can't found something that works for me.

This is my checkbox:

<input type="checkbox" class="main_account_form_checkbox" id="terms" name="terms" />

I need validate this checkbox, but do not know what I'm doing wrong. This is the steps in js file I'm working on:

Field: var terms = $('#terms');

On blur: terms.blur(validateTerms);

Checking:

form.submit(function(){
    if(validateTerms())
        return true
    else
        return false;
});

And the function:

function validateTerms(){
    if(terms.val().checked(false)){
        alert("Error!");
    }
}

Why I can't verify the checkbox? All other fields works except this. Can anyone help me please?

Upvotes: 0

Views: 127

Answers (2)

lee_gladding
lee_gladding

Reputation: 1100

Use checked property.

So to see if not checked

if(!terms.checked){
    alert("Error!");
}

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

Don't check the .val(), just use is(":checked")

if (terms.is(":checked")) //is checked

Upvotes: 5

Related Questions