简体   繁体   中英

Jquery Validate Single message on multiple rule failure

I am using Jquery Validate to validate my form.

Here is my rules JSON

{ 
   "maxlength":"10",
   "email":"true",
   "minlength":"6"
} 

and here is my messages JSON

{ 
   "maxlength":"Last Name length must be less than 10 characters",
   "email":"Last Name is an invalid email address.",
   "minlength":"Last Name length must be greater than 6 characters"
}

This gives one error message if a respective rule fails.

What I want is, if someone types "a", then it should fails 2 rules ie minlength and email, so at that time the message should be

Last Name length must be greater than 6 characters and Last Name is an invalid email address.

Is there a way to achieve this?

Create a loop that validates any rule, and then add any error to an array (in the example, I used errors as the array). After finishing the loop contents, use a similar code to handle the error codes, and join them by using JavaScript's join() function with the word "And" (+spaces) as the devider:

error_message = errors.join(' And ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM