如何使用数据库检查js中的电子邮件验证

时间:2015-02-19 11:44:37

标签: php codeigniter duplicates email-validation

我试图避免在MySQL数据库中添加重复的电子邮件地址。是否有任何javascript方式检查没有重新加载?就像在Gmail登录表单中一样?

1 个答案:

答案 0 :(得分:0)

使用验证库:http://docs.jquery.com/Plugins/Validation/Methods/remote

你的javascript:

$("#yourFormId").validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: "checkmail.php",
                    type: "post"
                 }
            }
        },
        messages: {
            email: {
                required: "Please Enter Email!",
                email: "This is not a valid email!",
                remote: "Email already in use!"
            }
        }
    });

checkmail.php:

<?php
$registeredEmails = array('test1@test.com', 'test2@test.com', 'test3@test.com');

$requestedEmail  = $_POST['email'];

if( in_array($requestedEmail, $registeredEmails) ){
    echo 'false';
}
else{
    echo 'true';
}
?>

你可以搜索mysql发布的电子邮件地址而不是$ registeredEmails数组:)