To check Form has at least one field checked in except hidden fields

183 views Asked by At

I am using Thymeleaf + HTML 5. In Search page, it has around 40 fields of text, list and checkbox fields. Below is my code to check whether atleast one field is filled in.

$(document).ready(function() {
    searchActionURL = function(obj) { 
        var atleastOneFilled = checkFields($("#searchForm"));
        if(atleastOneFilled)
        {
            $("#searchForm").attr("action", obj);                       
            $("#searchResults").show();
        }
        else
        {
            alert('Please specify atleast one search item');
        }
    }
    searchActionURL();

    function checkFields(form) {
        var checks_radios = form.find(':checkbox, :radio'); 
        var inputs = form.find(':input').not(checks_radios).not('[type="submit"],[type="button"],[type="reset"]'); 
        var checked = checks_radios.filter(':checked');
        var filled = inputs.filter(function(){ 
                return $.trim($(this).val()).length > 0;
            }); 
        if(checked.length + filled.length === 0) {
            return false;
        } 
        return true;
    }
} 

I have a hidden field of csrf token which never gets cleared from form. So how can I add exception of csrf token hidden field to my code. So that I can validate whether user entered atleast one search criteria. My current code always says there exists one field(pointing the hidden csrf field) even if I clear all fields in the form. Can any one help me on this issue.

1

There are 1 answers

0
Java_User On

I modified the code to exclude hidden fields.

var inputs = form.find(':input').not(checks_radios).not('[type="submit"],[type="button"],[type="reset"],[type="hidden"]');