dongya5893 2014-08-09 15:10 采纳率: 100%
浏览 23
已采纳

PHP验证无效

My php code is not working. It is showing wrong messages. For example if I fill the email address longer than 10 characters, then it still shows that my field is empty.

<?php
    $errors = array();

    if (isset($_POST['submit'])) {
        $name  = htmlentities(mysql_real_escape_string($_POST['name']));
        $email = htmlentities(mysql_real_escape_string($_POST['email']));
        $ip    = $_SERVER['REMOTE_ADDR'];

        if (strlen($email) > 10) {
            echo 'ok';  
        } 
        else {
            echo 'empty';
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • douyiyang6317 2014-08-09 15:19
    关注

    if you want to validate if the email address is 10 or under 10 -> error then u should use

    if(strlen($email)<=10) {
    echo 'ok';  
    } else {
    echo 'empty';
    }
    

    but there are other email validators that are better

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // invalid emailaddress
    }
    

    because what is my email is 1234567890abcdsed@mydomain.com then it will validate false but if thats my email adress then i have a problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?