2014-02-24 48 views
-2

我有一個註冊表單,我使用下面的代碼驗證表單客戶端,但問題是我輸入的密碼顯示的字段不匹配。我也檢查存在的電子郵件地址,這也是不工作請找到我的代碼片段,並告訴我在哪裏做錯了。jquery檢查現有的電子郵件地址進行表單驗證

$('#UserSignupForm') 
    .validate({ 
     rules : "data[User][firstname]" : "required", 
       "data[User][lastname]" : "required", 
       "data[User][email]" : { 
          required : true, 
          email : true, 
          remote : baseUrl + '/checkEmail' 
       }, 
       "data[User][password]" : { 
          required : true, 
          minlength : 5 
       }, 
       "data[User][confirm_password]" : { 
          required : true, 
          minlength : 5, 
              //equalTo : "#UserPassword" 
       } 
       }, 

       messages : { 
        "data[User][firstname]" : "firstname is missing*", 
        "data[User][lastname]" : "lastname is missing*", 
        "data[User][email]" : { 
         remote : "This email is already taken", 
         email : "Please enter a valid email address" 
        }, 
        "data[User][password]" : { 
         required : "Please provide a password", 
         minlength : "missing*" 
        }, 
        "data[User][confirm_password]" : { 
         required : "Please provide a password", 
         minlength : "Your password must be at least 5 characters long", 
        } 
       }, 

檢查唯一的用戶名我在做下面的代碼在我的控制器

public function checkEmail(){ 
    $this->autoRender = false; 
    $this->loadModel('UsersEntity'); 
    $email = $this -> request->data['User']['email']; 

    $sql = $this->User->query("Select username FROM users_entity WHERE username = {$email}"); 
    if($sql){ 
     return true; 
    } 
} 
+0

你有一個括號太多,在你的評論後jquery; // equalTo:「#UserPassword」 – pythonian29033

+1

@ pythonian29033,不是。它只是以這種方式出現,因爲在'rules:'之後的右括號缺失。應該是'規則:{...},消息:{...}' – Sparky

+0

對不起,然後 – pythonian29033

回答

1

您有一個語法錯誤...您錯過了rules內的左括號。

$('#UserSignupForm').validate({ 
    rules : {      // <-- opening brace was missing 
     "data[User][firstname]" : "required", 
     "data[User][lastname]" : "required", 
     "data[User][email]" : { 
      required : true, 
      email : true, 
      remote : baseUrl + '/checkEmail' 
     }, 
     "data[User][password]" : { 
      required : true, 
      minlength : 5 
     }, 
     "data[User][confirm_password]" : { 
      required : true, 
      minlength : 5, 
      //equalTo : "#UserPassword" 
     } 
    }, 
    messages : { 
     "data[User][firstname]" : "firstname is missing*", 
     "data[User][lastname]" : "lastname is missing*", 
     "data[User][email]" : { 
      remote : "This email is already taken", 
      email : "Please enter a valid email address" 
     }, 
     "data[User][password]" : { 
      required : "Please provide a password", 
      minlength : "missing*" 
     }, 
     "data[User][confirm_password]" : { 
      required : "Please provide a password", 
      minlength : "Your password must be at least 5 characters long", 
     } 
    } // <-- remove trailing comma 
}); // <-- closing braces for '.validate()' 
0

您的SQL查詢應該返回錯誤。因爲你必須在$ email變量上添加引號。

$sql = $this->User->query("Select username FROM users_entity WHERE username = {$email}"); 
to 
$sql = $this->User->query("Select username FROM users_entity WHERE username = '$email'"); 
+0

我同意你的意見,但這應該是一個評論,而不是一個答案嘿 – pythonian29033