PHP and Javascript Email Validation of '@' Symbol

后端 未结 4 872
暗喜
暗喜 2021-01-25 01:21

Trying to get code to search if \'@\' symbol is present in typed-in email address and echo if symbol is not. Everything works fine without the searching for @ code.

chec

相关标签:
4条回答
  • 2021-01-25 01:35

    try this :-

    function isValidEmailAddr ($email) {
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
          return true;
        } else {
          return false;
        }
    }
    

    usage :-

    include('path/to/lib.php');
    $test = 'email@foo.bar';
    if(isValidEmailAddr($test) == true){
     echo "good to go";
    } else { echo "try again babe!" }
    
    0 讨论(0)
  • 2021-01-25 01:38

    It will not only validate email address, but also sanitize it for unexpected characters:

    $email = $_POST['email'];
    $emailB = filter_var($email,   
     FILTER_SANITIZE_EMAIL); 
     if(filter_var($emailB,   
     FILTER_VALIDATE_EMAIL) === false ||  
     $emailB != $email ) { echo "This email adress 
      isn't valid!"; exit(0);
    }
    
    0 讨论(0)
  • 2021-01-25 01:46

    A much better approach to validating the emails, would be to use the built in filters intended for that sort of thing

    <?php
    
        include '/connect.php'; //connects to mysql
    
        $email = mysqli_real_escape_string($connect, $_POST['email']);
    
        $check = mysqli_query($connect, "SELECT email FROM users WHERE email='$email'");
        $check_num_rows = mysqli_num_rows($check);
    
        if ( filter_var($email, FILTER_VALIDATE_EMAIL) ) {
            if ($check_num_rows==0) {
                echo 'Email valid';
            } elseif ($check_num_rows==1) {
                echo 'Email already registered';
            }
        } else {
            echo 'Please enter a valid email';
        }
    ?>
    

    The reason your code doesn't work, is because PHP doesn't have a str or indexOf method, you should be using something like strpos instead

    if (strpos($email, '@') === false) { ...
    
    0 讨论(0)
  • 2021-01-25 01:53

    you can use default php email validation

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
      echo("$email is a valid email address");
    } else {
      echo("$email is not a valid email address");
    }
    ?>
    

    you can also check this at https://eval.in/870776

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