所以在表格中我有这个输入
<input type="text" name="squareFoot" value="<?PHP if(isset($_POST['squareFoot'])) echo $squareFoot ?>"><span class="error_message"><?PHP echo " " . $squareFootError; ?></span>
这是我的验证(在表格之上是肯定的)
if(isset($_POST['submit'])){
$isSubmitted = true;
$squareFoot = $_POST['squareFoot'];
$squareFoot = filter_var($squareFoot, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$squareFoot = filter_var($squareFoot, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
$squareFoot = filter_var($squareFoot, FILTER_SANITIZE_SPECIAL_CHARS);
if(!is_numeric($squareFoot)){
$isValid = false;
$squareFootError = "Please enter a numeric value";
}
else if(empty($squareFoot)){
$isValid = false;
$squareFootError = "Please enter a numeric value";
}
else if($squareFoot < 200){
$isValid = false;
$squareFootError = "Please enter a number between 200 and 500,000";
}
else if($squareFoot > 500000){
$isValid = false;
$squareFootError = "Please enter a number between 200 and 500,000";
}
else{
/// do math (code not shown)
// Format Square Footage
$squareFootFormat = number_format($squareFoot, 0, '', ',');
// Display to user
<p>1. Square Footage being stripped <span class="right_al"><?PHP echo $squareFootFormat; ?></span></p>
所以我设置它以便用户不能输入html或脚本,用户必须输入一个必须在两个数字之间的数字,并且该数字可以有一个逗号。
我还希望用户能够输入500.5这样的东西,但是当测试500.5变成5,005时。
是因为 $ squareFootFormat = number_format($ squareFoot,0,&#39;&#39;,&#39;,&#39;);
或者它有什么不对吗? 我有点想保留number_format(),因为它使数字更容易阅读,如果它有一些像100,000这样的大数字。我能这样做吗? 谢谢你的帮助。
答案 0 :(得分:1)
您的filter_var不允许将500.5作为值。
$squareFoot = filter_var($squareFoot, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
答案 1 :(得分:1)
仅这样做:
<?php
$squareFoot = $_POST['squareFoot'];
$smallest = 1;
$greatest = 100000;
if(is_numeric($squareFoot)) {
if($squareFoot < $greatest && $squareFoot > $smallest) {
//do what you want
echo number_format($squareFoot, 1, '.', ',');
}
else {
echo "Please enter a number between " .$smallest . " and ".$greatest;
}
}
else {
echo "Please enter a numeric value";
}
?>
对我来说看起来更简单。