表格编号验证

时间:2013-06-26 20:15:03

标签: php javascript forms validation

我正在尝试对Quantity进行验证它应该运行Function验证,以确保字段中的任何内容不小于1或具有小数或字母或符号。 它应该有一个警告说Quantity should not be less than 1

FORM

<Form method="post" action="qty.php">
    <tr>
        <td>Quantity</td>
        <td><input type="text" name="Qty" id="Qty" value="1" /></td>
    </tr>   
    <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
    <button type="submit" name="submit" class="show_hide" id="submit" name="submit">add</button>

</form>

低于

的东西

的javascript

function nonzero(val,wishlist)
{
    if(val.value<1 || val.value=='')
    {
        alert("Quantity should not be Zero or Null")
        val.value='1';
        val.focus();
        return false;
    }
    if(wishlist==1){
        document.getElementById('option').value='wishlist';
        document.product26.submit();
    }
    else
    {
        return option_add('26');
    }
    //return true;
} 

2 个答案:

答案 0 :(得分:1)

  1. 您可以使用HTML 5

    <input type="number" name="Qty" id="Qty" value="1" min="1" />

  2. 然后使用Javascript。

  3. 然后(并且总是这样做)写一些PHP进行双重检查。

    $qty = $_POST['qty'];
    if(!is_int($qty) || intval($qty) < 1) { echo 'Please provide an number only.';
    } else { $qty = intval($qty); // .... Etc }

答案 1 :(得分:0)

如果数量是INT,只需检查php:

  $qty = $_POST['qty'];

  if(!is_int($qty) || $qty < 0 ){
    echo 'Please provide an positive number only.';  
  }else{
    echo 'Im a number';
  }