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.
whileloop, then you will be outputting multiple forms, and creating multiple form elements with the sameidattribute. Please show more of your PHP code.