tree em
tree em

Reputation: 21781

jquery form validation plugin need help?

    </tr>
for ($i = 0; $i < $dm_numrec; $i++) {
?>
         <tr>
           <td width="266" height="28" valign="top">
             <input type="text" name="recommend_to_name<?php echo $i;?>" />
           </td>
           <td height="28" valign="top">
             <input type="text" name="recommend_to_email<?php echo $i;?> />" 
           </td>
         </tr>

I used jquery form validation plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/

<script src="jquery.min.js"></script>

<script src="jquery.validate.min.js"></script>

        <script>
            $(document).ready(function(){           
                $('#recommend').validate({
                    'rules':{
                        'recommend_to_name':'required',
                        'recommend_to_email':{
                            'required':true,
                            'email':true
                        },              
                    }
                });

            });

        </script>   

How can I validate for the recommend_to_name that its name is a dynamic string from loop:

recommend_to_name0

recommend_to_name1

recommend_to_name2

recommend_to_name..

Upvotes: 1

Views: 259

Answers (3)

ChadT
ChadT

Reputation: 7713

See my previous question here: jquery validate on elements not yet created

Basically you use the:

rules( "add", rules ) 

Method to programatically create rules on the fly for the form fields you are creating. See this page for further documentation: http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules

Edit: more info:

In your loop:

 for ($i = 0; $i < $dm_numrec; $i++) {
    $("input[name=recommend_to_name<?php echo $i;?>").rules("add", {
         required: true,
         minlength: 20
    });
  ...

for instance. So basically what you are doing here, is creating some rules on the fly for each of the elements that are created.

It might be better to use classes, as another user pointed out.

Upvotes: 1

Sinan
Sinan

Reputation: 5980

you can use a class as selector instead of a id. Then you can use something like addClassRules function

Upvotes: 0

Related Questions