使用远程方法验证电子邮件

时间:2015-03-05 06:26:31

标签: php validation

我希望以正确的格式验证电子邮件字段

  function editAccount() {
             $("#editacc").validate({
               rules: {
            email: {
                remote: {
                    url: "../accounts/check-email.php?type=email",
                    type: "get",

                    data: {
                      email: function() {
                        return $( "#email" ).val();
                      },

                      id:function(){
                        return $( "#userid" ).val();
                      }

                    }
                  }               
            },
            imeino: {
                remote: {
                    url: "../accounts/check-email.php?type=imeino",

                     number: true,
                    type: "get",
                    data: {
                      imeino: function() {
                        return $( "#imeino" ).val();
                      },
                      id:function(){
                        return $( "#userid" ).val();
                      }
                    }
                  }               
            },
            username: {
                remote: {
                    url: "../accounts/check-email.php?type=username",
                    type: "get",
                    data: {
                      imeino: function() {
                        return $( "#username" ).val();
                      },
                      id:function(){
                        return $( "#userid" ).val();
                      }
                    }
                  }               
            },
            mobileno: { 

                     minlength: 10,
                    number: true    
            },
            accounttype:"accountcheck"
        },


messages:{
            email: {
                email: "Invalid Email Address",
                remote: "Email ID Already registered"

            },
            imeino: {
                remote: "IMEI No Exists",

            },
            username: {
                remote: "Username Exists"
            },
            accounttype:"Imei no required for Agent",
            firstname:"Firstname Required",
            lastname:"Lastname Required",
            mobileno:"Mobile Number Required with numbers",
            email:"Email Id Required with valid format!!",
            reportingstaff:"Reporting Staff Required"

    },

电子邮件未以正确格式验证。

1 个答案:

答案 0 :(得分:1)

    $( "#myform" ).validate({
      rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "check-email.php",
            type: "post",
            data: {
              email: function() {
                return $( "#email" ).val();
              }
            }
          }
        }
      }
    });

/ *************** check-email.php ************** /

< ?php

/* check if email is already registered */

//connect to db using mysqli

if (!empty($_POST['email']))
{
    $email = $mysqli->real_escape_string($_POST['email']);
    $query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
    $results = $mysqli->query($query);
    if($results->num_rows == 0)
    {
        echo "true";  //good to register
    }
    else
    {
        echo "false"; //already registered
    }
}
else
{
    echo "false"; //invalid post var
}

?>