简体   繁体   中英

validate array of values from select box using jquery validator

I want to validate each select box .Number of select box is depend on the value retrieved from the databse.. My code is given below.. But Its not working...

<script>
    $(document).ready(function () {
        $("#multipleForm").find("status").each(function () {
            $(this).validate({
                rules: {
                    'status[]': {
                        required: true
                    }
                }
            });
        });
    });
</script>

<form role="form" enctype="multipart/form-data" method="post" action="update.php" id="multipleForm">
    <?php $cl=m ysql_query( "select * from demo where id='$cid'"); while($v2=mysql_fetch_array($cl)){ ?>
    <label>
        <?php echo $v2[ 'details'] ; ?>
    </label>
    <textarea name="remarks[]"></textarea>
    <label>Status</label>
    <select name="status[]">
        <option value="">select status</option>
        <option value="NC">yes</option>
        <option value="PC">no</option>
    </select>
    <?php } ?>
</form>`

can any one help me please to fix this problem?

Here

$("#multipleForm").find("status")

you have used status as a tag whereas it is not.

Try this instead - select[name=status]

ie $("#multipleForm").find(" select[name=status]" )

When using jQuery validator plugin, you can also do one thing ie you can validate the form instead of selective elements and just use necessary class names for different elements

eg <select class='required'>...</select>

and

$("#formid").validate();

Update

Try this -

 $("#multipleForm").validate({
                    rules: {
                        'status': {
                            required: true
                        }
                    }
            });

Note:

Make sure that the validator plugin is being loaded in proper order and .validate() is ready to be used.

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