简体   繁体   中英

Check the value of a text input

I have a PayPal button with a quantity text field. How could I check to ensure this textfeild is > 0 so that it does not add to cart if quantity is not an integer >= 1?

Paypal carts are smart enough not to count negative orders. But you could also provide some javascript logic to your client-side that would prevent the action from taking place if the value is less than 1.

A bit of Javascript/jQuery as an example:

$("submit").click(function(e){
  var qty = $(this).closest("form").find("[name='qty']").val();
  if (qty < 1) {
    e.preventDefault();
  }
});
if ($_POST['field_name'] > 0) { ... }

For this I normally use:

if ( isset($_POST['quantity']) 
     && preg_match('/^[1-9]\d*$/', $_POST['quantity'] ) {
}

the first test ensure you don't trigger an error if the quantity isn't in the $_POST array. the second ensures that the string has only digits and the first isn't zero.

This may help:

if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

http://php.net/manual/en/function.empty.php

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