1

Here's the jQuery:

$(function() {
    $("#register-form").validate({
     rules: {
        fname: "required",
        surname: "required",
        middlename: "required",
       },
    messages: {
        fname: "Please enter your name",
        surname: "Please specify your surname",
        middlename: "Please specify your surname",

    },
          submitHandler: function(form) {
        form.submit();
     }

    });
});

Here's my HTML:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"  method="post"     id="register-form">
<table width="614" border="0">
<tr style="<?php echo $errorname_css; ?>">
<td width="153">First Name</td>
<td width="175">
  <input type="text" name="fname" value="<?php echo $fname; ?>" /></td>
<td width="272">*<?php echo $fname_err; ?></td>
 </tr>
 <tr style="<?php echo $errorsurname_css; ?>">
 <td>Surname</td>   
<td><input type="text" name="surname" value="<?php echo $surname; ?>" /></td>
<td>*<?php echo $surname_err; ?></td>
</tr>
 <tr  style="<?php echo $errormidname_css; ?>">
<td>Middle Name</td>
<td><input type="text" name="middlename" value="<?php echo $middlename; ?>"  /></td>
<td>*<?php echo $middlename_err; ?></td>
</tr>
<tr  style="<?php echo $errormidname_css; ?>">
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit" /></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

I would like to put the css inside the Jquery messages, but i dont know how, can someone help me with this? really appreciated, thanks :)

1
  • put somethin in a fiddle. Commented Jan 19, 2014 at 10:08

1 Answer 1

1

You can use the hightlight/unhighlight methods

$("#register-form").validate({
    rules: {
        fname: "required",
        surname: "required",
        middlename: "required",
    },
    messages: {
        fname: "Please enter your name",
        surname: "Please specify your surname",
        middlename: "Please specify your surname",

    },
    highlight: function (element, errorClass, validClass) {
        $(element).closest('tr').addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).closest('tr').addClass(validClass).removeClass(errorClass);
    },
    submitHandler: function (form) {
        form.submit();
    }
});

then

.error {
    color: red;
}

Demo: Fiddle

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

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.