最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery validation multiple error messages - Stack Overflow

programmeradmin4浏览0评论

I have a scenario where I have two type of validations done on a field. I want error messages from both of the validation to be displayed at the same time.

E.g: I have an email field and added class "required email" to field and configured messages object to display 2 different message. Now if email field is empty, it gives only 1 error from required. I want both of the messages to be displayed when they are not validated.

/

In the following example, submit the form without entering anything in the field. It displays a message that "Email is required field". But I want to display messages from both of the validations.

I have a scenario where I have two type of validations done on a field. I want error messages from both of the validation to be displayed at the same time.

E.g: I have an email field and added class "required email" to field and configured messages object to display 2 different message. Now if email field is empty, it gives only 1 error from required. I want both of the messages to be displayed when they are not validated.

http://jsfiddle/emphaticsunshine/JcFYr/

In the following example, submit the form without entering anything in the field. It displays a message that "Email is required field". But I want to display messages from both of the validations.

Share Improve this question edited Mar 19, 2012 at 18:29 emphaticsunshine asked Mar 19, 2012 at 18:15 emphaticsunshineemphaticsunshine 3,7857 gold badges34 silver badges42 bronze badges 8
  • What have you tried so far? Is your validation breaking out as soon as one part is invalid or does it check for each error before returning all of the error messages. Your code would help as we have no idea what you are doing. – jzworkman Commented Mar 19, 2012 at 18:19
  • 1 give me a few mins...i will just show you an example – emphaticsunshine Commented Mar 19, 2012 at 18:21
  • Please go to the above link and let me know if you are still not able to understand what I am exactly looking for. – emphaticsunshine Commented Mar 19, 2012 at 18:30
  • 2 Is there a reason you want to show both messages? The validation will check if the field is empty and return the first message, then it will check if it is valid(only if they actually entered something) Why do you want to spam the user with two different error messages when one will suffice? – jzworkman Commented Mar 19, 2012 at 18:32
  • displaying both messages doesn't make sense... – c0deNinja Commented Mar 19, 2012 at 18:35
 |  Show 3 more ments

2 Answers 2

Reset to default 3

The validation checks each step in order, and returns the first problem. Imagine having 10 validations(not really feasible but bear with me) and the first fails, do you really want the performance hit of checking the other 9 conditions when the input is already invalid. Explain this to your client and explain the user experience. If you want to fake displaying both for your clients requirements just add the second error to the first error text(because the only time both would show is when it is an empty input)

http://jsfiddle/JcFYr/1/

To do this, I think your best bet is to make another validation test for the condition where all are failed. Do this check first, and if that's met, show the error message for both. If it passes (both don't fail) then test for the other two.

发布评论

评论列表(0)

  1. 暂无评论