dongyongmin5711 2017-09-29 04:32 采纳率: 100%
浏览 110
已采纳

PHP和Javascript电子邮件验证'@'符号

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.

check.php

<?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 ($email==NULL) {
    echo 'Enter an email';
} elseif (strlen($email)<6) {
    echo 'Please enter a valid email';
} elseif (str($email).indexOf('@') == -1) {
    echo 'Please enter a valid email';
} else {
    if ($check_num_rows==0) {
        echo 'Email valid';
    } elseif ($check_num_rows==1) {
        echo 'Email already registered';
    }
}

register.php

<script type="text/javascript" src="/jquery-3.2.1.min.js"></script>

<script>
    $(document).ready(function() {
       $('#feedback_email').load('/check.php').show();

       $('#email').keyup(function() {
           $.post('/check.php', { email: form.email.value }, 
           function(result) {
               $('#feedback_email').html(result).show;
           });
       });
    });
</script>

<form action="register.php" method="post" name="form">

<input id="email" type="text" name="email" placeholder="Email Address" maxlength="60" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>">

<div id="feedback_email"></div>

<p><center><input type="submit" name="submit" value="Register"></center>
</form>

EDIT: I do have a built in filter for the php back-end validation. However, I'm trying to do a front-end validation as well through these codes. If anyone knows how to fix my script please post. I'm receiving error

Call to undefined function str() in /check.php on line 13

Line 13

} elseif (str($email).indexOf('@') == -1) {
  • 写回答

4条回答 默认 最新

  • dtuy84033 2017-09-29 04:39
    关注

    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) { ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图