0

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:{
                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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.