1

So I am using the jQuery validation plugin to validate textboxes in a php while loop on one of my pages.

The problem that I am having is that the validation only works for the first textbox and not for the rest.

I have tried assigning a unique key rather than validating it with the id of the form, but so far I have not had any luck. It's probably because I am assigning it incorrectly I think.

Here is my script for jQuery:

<script type="text/javascript">
jQuery(document).ready(function($) {
$.validator.addMethod('minNum', function (value, el, param) {
return value > param;
},
"Enter a number greater than 0!");
$("#quantityInputUser").validate({
    rules: {
        quantityInput: {
            required: true,
            minNum: 0,
            number: true,
            maxlength:2
            }
        },
    messages: {
        quantityInput: {
            required: "The quantity is required!",
            minNum: "Enter a number greater than 0!",
            number: "Enter a number!",
            maxlength: "Quantity must be between 1-99!"
        },
    }
});         
});

 </script>

And here is the form in the while loop:

 while($row2 = mysqli_fetch_array($itemQuery3)){
                                //some variables are assigned

                                //other stuff happens

        echo "<form action='menu.php' method='post' id='quantityInputUser'>";
                        echo '<div class="row"><h5> Quantity: </h5><input type="number" id="text" maxlength="2" name="quantityInput" value="'.$qty.'"/></div></div>';

         echo "<div class='row'>
         <input class='send' type='submit' name='updateCart' value='Update Cart'/>
         <input name='itemAdjust' type='hidden' value='$itemID'/>
         </form></div>";

}

If anybody can point me in the direction of how I would go about assigning a unique key in the form and then referencing it in the script file, it would be greatly appreciated.

EDIT: Apologies if I didn't make it clear, the forms are created in the while loop, therefore there are multiple forms.

2
  • WHere is the while loop? Are there multiple form? Commented Apr 28, 2015 at 12:05
  • If the PHP code you posted is in your while loop, then you will be outputting multiple forms, and creating multiple form elements with the same id attribute. Please show more of your PHP code. Commented Apr 28, 2015 at 12:07

1 Answer 1

2

The other answer is actually pretty close. You should change it to be a class, like so:

echo "<form action='menu.php' method='post' class='quantityInputUser'>";

But then your javascript should be more like this:

$(".quantityInputUser").each(function(){
    $(this).validate({
    .... rules ....
    });
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.