简体   繁体   English

检查文本输入的值

[英]Check the value of a text input

I have a PayPal button with a quantity text field. 我有一个带有数量文本字段的PayPal按钮。 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? 我如何检查以确保此textfeild> 0,以便在数量不是大于等于1的整数时不会将其添加到购物车中?

Paypal carts are smart enough not to count negative orders. 贝宝(Paypal)购物车足够聪明,不会计算负面订单。 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. 但是您还可以向客户端提供一些JavaScript逻辑,如果该值小于1,则会阻止该操作发生。

A bit of Javascript/jQuery as an example: 以Javascript / jQuery为例:

$("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. 第一个测试确保如果quantity不在$_POST数组中,则不会触发错误。 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 http://php.net/manual/en/function.empty.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM