Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please note that any validation you do with JavaScript (with or without jQuery) should be repeated in your PHP code (obviously the syntax won't be the same), because you can't ever completely trust what is submitted by the browser (the user might have used dev tools to bypass your client-side validation).</p> <p>Having said that, to answer your question on how to do the validation you've described with jQuery, given a variable <code>Qty</code> holding the user input you can test whether it is an integer between 1 and 99 using the following code:</p> <pre><code>if (!/^[1-9]\d?$/.test(Qty)){ alert('Add meaningful error message here'); return false; } </code></pre> <p>The regex I've shown, <code>^[1-9]\d?$</code>, allows the first character in the string to be a digit between 1 and 9, and then an optional second character that is any digit (0-9). Which works out to all of the numbers between 1 and 99. (Actually that was just plain JS, not jQuery.)</p> <p>In the context of your existing code:</p> <pre><code>$(document).ready(function () { $('#button').click(function () { var pid = $('#pid').val(); var length = $('#length').val(); var Qty = $('#Qty').val(); // this is the quantity var Category = $('#Category').val(); if (!/^[1-9]\d?$/.test(Qty)){ alert('Add meaningful error message here'); return false; // don't continue } $.ajax({ url: 'cart.php', type: 'POST', data: { pid: pid, length: length, Qty: Qty, Category: Category }, success: function (data) { } }); }); }); </code></pre> <p>Note that the PHP code that you showed in your question (that you later edited into the middle of your JS) doesn't make sense because it actually <em>changes</em> the value entered by the user to force it to meet the rules. E.g., user types "15.0" so your code removes the decimal to leave "150" and then decides that that is too big and makes it "99". So the user thinks they've sent you "15.0" and you've changed it to "99".</p> <p>Note also that if you know that 99 is the largest permitted number then you can add <code>maxlength="2"</code> to the input element in question so that the user can't type more than two characters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload