doutao1939 2015-05-04 07:48
浏览 79
已采纳

验证只接受浮点数,但它也接受整数,在PHP?

In the form the user needs to enter two values. These values should only float numbers. I have searched how to validate to input float values, and the way I did it, it accepts integers. I used the filter_var() function with the FILTER_VALIDATE_FLOAT . It accepts successfully the float scores, but it also accepts the integers which I don't want it.

savelibscores.php

<?php

 define('DB_NAME','');
 define('DB_USER','');
 define('DB_PASSWORD','');
 define('DB_HOST','localhost');

 $connect = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

 if(!$connect){
    die('Could not connect:'.mysql_error());
 }

 $db_selected=mysql_select_db(DB_NAME,$connect);

 if(!$db_selected){
    die('Can\'t use'.DB_NAME.':'.mysql_error());
 }

if(isset($_POST['submit'])){

$value1=$_POST['s3'];
$value2=$_POST['s4'];
$value3=$_POST['year'];


if(filter_var($value1,FILTER_VALIDATE_FLOAT) && filter_var($value2,FILTER_VALIDATE_FLOAT)) {
    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.";
}


}
?>
  • 写回答

3条回答 默认 最新

  • dongyan7988 2015-05-04 07:53
    关注

    Floats are a superset of integers, and as a float input, "3" is just as valid as "3.14159", even though 3 also so just happens to be a valid integer as well.

    Would you rather force people to enter "3.0" instead of "3"? The resulting float value will be exactly the same, and you're only making it less convenient for your users.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下