简体   繁体   中英

jQuery check if at least two text inputs are filled

This is my form :

<FORM id="form1">                          
            Fields :
                        <fieldset name="titles">
                        <br>
            <INPUT class = "fit" type="text" name="title">
                        <br>
            <INPUT class = "fit" type="text" name="title">
                        <br>
                        <INPUT class = "fit" type="text" name="title">
                        <br>
                        <INPUT class = "fit" type="text" name="title">                     
                        </fieldset>
            </FORM>

How can I check if at least two out of these four fields are filled? Here's what i'I've tried :

$('#form1').validate({ // initialize the plugin
    rules: {
        title: {
          required: "input[name='title']",
          minlength: 2
      },
      messages: {
            title: "You must fill at least two titles!"
        }
    }
}); 

But it's not working, it always returns a valid form... I'm a newbie to jQuery, any help would be appreciated.

For 1 statement to grab both 'select' and 'input' elements, simply change your single jQuery selector to a multiple selector, Eg:-

$(this).find('input[type=text], select').each(function(){
            if($(this).val() != "") valid+=1;
        });

Try this with the button:

$("#submit_button").click(function(){
        if($('input[text]').length < 2) {
                alert('at least two text inputs are filled');
               return false;
            }
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM