1

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.net/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.

emphaticsunshine
  • 3,725
  • 5
  • 32
  • 42
  • 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 Mar 19 '12 at 18:19
  • 1
    give me a few mins...i will just show you an example – emphaticsunshine Mar 19 '12 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 Mar 19 '12 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 Mar 19 '12 at 18:32
  • displaying both messages doesn't make sense... – c0deNinja Mar 19 '12 at 18:35
  • I understand, but there should be an option to display both messages. This is the requirement of my client, so I came across this problem. You are right considering user experience, but I feel like there must have been some option to run all the validations and display message. – emphaticsunshine Mar 19 '12 at 18:36
  • I clearly understands your point regarding performance. But these requirements are stupid so I was wondering if there was something already in box for me. But I can do that myself. Anyways thank you for your suggestions. – emphaticsunshine Mar 19 '12 at 18:42
  • See this question this ticket : http://stackoverflow.com/questions/11860029/jquery-validation-plugin-how-to-add-multiple-custom-messages-in-custom-method Regards – user3544857 Apr 21 '15 at 07:50

2 Answers2

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.net/JcFYr/1/

jzworkman
  • 2,695
  • 14
  • 20
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.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
  • I think that will be waste of all the resources as mentioned in above answer. – emphaticsunshine Mar 19 '12 at 18:45
  • No doubt. But sometimes, client's requirements require just such a waste. If you only have to do it once, it's not so bad... but it's true, most of the time you only need to show one error message at a time. – Surreal Dreams Mar 19 '12 at 18:46
  • Yes, but if I have to do it, I will do it the way you mentioned above. – emphaticsunshine Mar 19 '12 at 18:50
  • If nothing else, my method lets you deliver a custom message that only shows on the failure of multiple conditions, as opposed to showing the other error messages stacked up together. – Surreal Dreams Mar 19 '12 at 18:54
  • @Surreal Dreams, Maybe you can help me. Look at this : https://stackoverflow.com/questions/47086843/how-can-i-make-two-message-in-password-confirmation-in-jquery-validate – moses toh Nov 03 '17 at 02:24