开发者

How to implement my validation, through jquery validation plugin? I am not able to validate my form?

This is my HTML page..

<div>
<form id="signupForm" method="get" action="">
<table> 
  <tr><td><input type="button" value="New" onclick="clearuserfields()"/></td></tr>
 <tr><td>USERID:</td><td><input type="text" id="userID"/></td></tr>
 <tr><td>FNAME:</td><td><input type="text" id="fname" name="fname" /></td></tr>
 <tr><td>LNAME:</td><td><input type="text" id="lname" name="lname" /></td></tr>
 <tr><td>EMAIL:</td><td><input type="text" id="email" name="email" /></td></tr>
 <tr><td>PASSWORD:</td><td><input type="text" id="password" name="password" /></td></tr>
 <tr><td>PHONENO:</td><td><input type="text" id="phoneno" name="phoneno" /></td></tr>
 <tr><td>ROLL:</td><td><input type="text" id="roll" name="roll" /></td></tr>
 <td><input type="button" value="Save" onclick="initialiseusers()"/></td>
 <td><input type="button" value="Delete" onclick="deleteuser()"/></td> 
 </table>
 </form>
</div>
this is my JS page
function initialiseusers(){
$("#signupForm").validate({
    rules: {
        fname: "required",
        lname: "required",
        email:{
            required: true,
            email: true
        },
        password:{
            required: true,
            minlength: 5
        },
        phoneno:"required",
        roll:{
            required: true,
            minlength: 2
        },      
        messages: {
            fname:"Please enter your firstname",
            lname:"Please enter your lastname",
            email:"Please enter a valid email address",
            password:{
              开发者_Python百科  required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            phoneno:"Please enter your phoneno",
            roll:"Please enter your role"
        }
    }
         updateusers();// when the validation is over this function being called for updating the user.
});

But the validation is not working and when i click the save button.It directly saves Blank values inside my DB I downloaded the validation plugin and added it in the path also.,Any suggestions please?


The validation plugin binds to the form's submit event, it's not meant to be called in-line. Instead setup the validation, then call .valid() to check it, like this:

$(function() {
  $("#signupForm").validate({
    rules: {
        fname: "required",
        lname: "required",
        email:{
            required: true,
            email: true
        },
        password:{
            required: true,
            minlength: 5
        },
        phoneno:"required",
        roll:{
            required: true,
            minlength: 2
        }
    },      
    messages: {
        fname:"Please enter your firstname",
        lname:"Please enter your lastname",
        email:"Please enter a valid email address",
        password:{
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        phoneno:"Please enter your phoneno",
        roll:"Please enter your role"
    }
  });
});
function initialiseusers(){
  if($("#signupForm").valid())
    updateusers(); //called if successfully validated
}

You can test it here, .validate() sets up the validation, it doesn't execute it, .valid() does, and returns a boolean of if it was successful.


Not sure how the validate plugin works, but you may need to return "false" from initialiseusers in order to prevent the default form submission from happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜