2014-03-28 109 views
0

我是PHP新手,我想在我的PHP項目中應用jQuery驗證。這裏是jQuery的代碼,你能找到本節中的錯誤。jQuery驗證規則不適用於PHP

(function($,W,D) 
{ 
var JQUERY4U = {}; 

JQUERY4U.UTIL = 
{ 
    setupFormValidation: function() 
    { 
     //form validation rules 
     $("#register-form").validate({ 
      rules: { 
       username: "required", 
       joindate: "required", 
       location: "required", 
       aboutme: "required", 
       fileupload: "required", 

       emailid: { 
        required: true, 
        email: true 
       }, 
       password: { 
        required: true, 
        minlength: 5 
       }, 
       agreement: "required" 
      }, 
      messages: { 
       username: "Please enter your username", 
       joindate: "please enter join date", 
       location: "please enter your location", 
       aboutme: "please write about urself", 
       password: { 
        required: "Please provide a password", 
        minlength: "Your password must be at least 5 characters long" 
       }, 
       emailid: "Please enter a valid email address", 
       fileupload: "Please upload a valid image", 
       agreement: "Please accept our terms and policy" 
      }, 
      submitHandler: function(form) { 
       form.submit(); 
      } 
     }); 
    } 
} 

//when the dom has loaded setup form validation rules 
$(D).ready(function($) { 
    JQUERY4U.UTIL.setupFormValidation(); 
}); 

})(jQuery, window, document); 
+1

歡迎SO。我不認爲'$(D).ready(函數)是正確的 –

+1

@JamesWong - 它是當你傳遞奇怪的論點給一個IIFE。 – adeneo

+0

這將有助於,如果你描述了什麼問題,並添加了HTML也。 –

回答

1

如果不是強制性的,那麼你可以避免jQuery或javascript驗證。正如有些人可以通過禁用瀏覽器javascript輕鬆完成。最重要的是你需要的PHP驗證,並使其「必需的」更安全的使用HTML5屬性限制用戶向前移動,而無需輸入值

Username: <input type="text" name="username" required /> 
Password: <input type="password" name="username" required /> 
Email: <input type="email" name="email" required /> 
URL: <input type="url" name="url"> 

您也可以通過將一個標題在它定製你的消息,

Username: <input type="text" name="username" required title="Please enter your username" /> 

您還可以將條件放在您想要驗證的方式上,請檢查此link

+0

感謝您的回答......它的工作原理:) –

+0

快樂編碼:) –

+1

這可能比JavaScript解決方案更健壯,但不是更安全 - 它不會更安全使服務器端驗證不那麼重要。 – halfer

0
$(document).ready(function(){ 

     $("#register-form").validate({ 
     rules: { 
      username: { 
       required: true      
      }, 
      joindate: { 
       required: true      
      }, 
      location: { 
       required: true      
      }, 
      aboutme: { 
       required: true      
      }, 
      fileupload: { 
       required: true      
      }, 
      emailid: { 
       required: true, 
       email: true 
      }, 
      password: { 
       required: true, 
       minlength: 5 
      }, 
      agreement: "required" 
     }, 
     messages: { 
      username: "Please enter your username", 
      joindate: "please enter join date", 
      location: "please enter your location", 
      aboutme: "please write about urself", 
      password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 5 characters long" 
      }, 
      emailid: "Please enter a valid email address", 
      fileupload: "Please upload a valid image", 
      agreement: "Please accept our terms and policy" 
     }, 
     submitHandler: function() { 
      document.register-form.submit(); 
     } 
    }); 
}); 

替換你上面的代碼和測試所有jQuery代碼,它會工作