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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器