views:

23

answers:

1

How to validate a email address field with three rules with three customized messages in the div container. ie.

rules: {
    email: {
        validationRule: true,
        email: true,
        remote: '/ajax/emailDuplicationCheck.php'
       }
     }

if first one false message should be "validation rule failed"

if second one false(fail) "enter email address"

if third(remote) failed. message should be "Account already exist in database".

I can added one message for all rules but i want to customize the message with respect to the rules.

A: 

Try this:

$("#myForm").validate({ // Replace #myForm with the ID of your form
    rules: {
        email: {
            required: true,
            email: true,
            remote: {
                url: "/ajax/emailDuplicationCheck.php",
                type: "post",
                data: { email: function() {
                    return $("#email").val(); // Add #email ID to your email field
                }
            }
        }
    },
    messages: {
        email: {
            required: 'Email address is required',
            email: 'Please enter a valid email address',
            remote: 'This email address has already been used'
        }
    },
    errorPlacement: function(error) {
        $("#response").html(error);
    }
});

Hope this helps !

FreekOne
No. it is not working. it is only displaying the message in email: 'enter email address'. As I mentioned. I want to display msg in div container?
The syntax is correct and that's the way it's supposed to be done, so the problem must be somewhere else. `validationRule` looks like a custom validation method. What exactly is it supposed to be doing ? Can you please edit your question and add the code for that method as well ?
FreekOne
validationRule method doing nothing. it is just return true. well if i remove that line. even than it is not working with two rules. and how can I display it in Divcontainer of id #response. I want to display the message in div container
Please see the updated answer. Also please note that in order to get the remote validation working, your `emailDuplicationCheck.php` file should listen for `$_POST['email']` and ONLY output `false` if the email is used, or `true` if the email is NOT used.
FreekOne
errorPlacement is the part which i was missing :) it works. thanks for help
Glad to be of help :)
FreekOne