如何验证只输入1.0 -5.0
的浮点数?我的系统现在可以接受:
+
,它不会显示在数据库中a
,它不会显示在数据库中。下面我有一些代码用于检查输入浮点数。
savelibscores.php
if(isset($_POST['submit'])){
$value1=$_POST['s3'];
$value2=$_POST['s4'];
$value3=$_POST['year'];
if((filter_var($value1,FILTER_VALIDATE_FLOAT)&& $value1>0) && (filter_var($value2,FILTER_VALIDATE_FLOAT) && $value2>0)) {
echo 'TRUE.';
} else {
echo 'FALSE.';
}
if(!empty($value1) && !empty($value2) && !empty($value3)){
$sql=mysql_query("INSERT INTO `library`(s3,s4,year) VALUES ('".$value1."','".$value2."','".$value3."')")or die(mysql_error());
}
else{
echo "Please fill all the fields. Please be sure to use float values also.";
}
}
答案 0 :(得分:2)
我是从PHP的网站http://php.net/manual/en/filter.examples.validation.php
和http://php.net/manual/en/filter.filters.validate.php
if(isset($_POST['submit'])){
$value1=$_POST['s3'];
$value2=$_POST['s4'];
$value3=$_POST['year'];
$options = array(
'options' => array(
'min_range' => -5,
'max_range' => 1,
)
if(!empty($value1) && !empty($value2) && !empty($value3)){
if((filter_var($value1,FILTER_VALIDATE_FLOAT,$options) && (filter_var($value2,FILTER_VALIDATE_FLOAT,$options)))
{
echo 'TRUE.';
$sql=mysql_query("INSERT INTO `library`(s3,s4,year) VALUES ('".$value1."','".$value2."','".$value3."')")or die(mysql_error());
} else {
echo 'Please be sure to use float values.';
}
} else {
echo "Please fill all the fields. Please be sure to use float values also.";
}
}