20

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.

2 Answers 2

15

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 !

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

9 Comments

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 ?
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.
@ToniMichelCaubet as I said in my second comment above, it should return true if the email is not used or false if the email is already in use.
|
5

You can use custom validation rules along with your own custom messages for each rule.

For simplicity, here's an example of how to validate a 'username' input with three custom methods for validation (each with its' own error message):

// Add three custom methods first:

$.validator.addMethod("nameCustom", function(value, element) {
    return this.optional(element) || /^[a-zA-Z ]+/.test(value);
}, 'Please use english letters only.');

$.validator.addMethod("noSpaceStart", function(value, element) { 
    return value.indexOf(" ") != 0; 
}, "Name cannot start with a space");

$.validator.addMethod("noSpaceEnd", function(value, element) { 
    return value.lastIndexOf(" ") != value.length - 1; 
}, "Name cannot end with a space");

$('#form_to_validate').validate({
    rules: {
        username: {
            // Add the custom validation methods to the username input
            noSpaceStart: true,
            noSpaceEnd: true,
            nameCustom: true,
            // required and maxlength are built-in methods in the plugin and are ready to be used
            required: true,
            maxlength: 50
        }
    }
});

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.