PHP empty field validation

前端 未结 4 1048
感情败类
感情败类 2021-01-14 05:48

I have a enrollment form where users have to input there address. I am using PHP for validation and currently I check to make sure the field is not empty. Here is my code:

相关标签:
4条回答
  • 2021-01-14 06:20
    if (empty($_POST['address']) || strlen(trim($_POST['address']))==0){
        $msg = 'You must enter an address' ;
    }else {
        $address = mysqli_real_escape_string($dbc, strip_tags(trim($_POST['address']))) ;
    }
    

    Trim the value.

    0 讨论(0)
  • 2021-01-14 06:21

    You already use trim() on the insert, why not call it once in the beginning, and test that value?

    $user_address = trim($_POST['address']);
    
    if (empty($user_address)) {
        $msg = 'You must enter an address' ;
    } else {
        $address = mysqli_real_escape_string($dbc, strip_tags($user_address)) ;
    }
    
    0 讨论(0)
  • 2021-01-14 06:31
    if($_POST['submit']){
        $address = $_POST['address'];
    
        if (empty($address)) {
            echo "You must enter an address";
        } else {
            $address = mysqli_real_escape_string($dbc, strip_tags($user_address)) ;
        }
    }
    

    You can echo immediately the message if the field address is empty

    also the $_POST['submit'] is to determine if the user click the submit button

    0 讨论(0)
  • 2021-01-14 06:44

    You need to consider regular expressions. I would start off using a split command: split(' ', $address).
    From that point, you can add one extra else if statement to test and see if the resultant string is empty.
    BUT, more importantly, you will now have the tools to test the entire address string to see if it contains: a two letter state code followed by a comma, and a 5(or9) digit zip code. (I can be more specific if you like.)

    0 讨论(0)
提交回复
热议问题