Note that there are some explanatory texts on larger screens.

plurals
  1. POClient side validation using javascript with JSF
    text
    copied!<p>I'm using JSF 2.0 and PrimeFaces library. I have a page with several inputs (among them there are 2 p:calendar components). I want to validate that the second date is not before the first date, but I want this validation to happen in the client, and if validation fails, then don't submit the form and display the h:message that belong to the calendars. PrimeFaces' calendar has a minDate attribute, but this just works not allowing to choose a previous date using the graphical calendar, but you can still type a previous date and validation passes; so I have to use javascript too. I can add a "onclick" event to the commandButon to call js function that performs validation, but how can I stop JSF from submitting the form is javascript validation failed? and how can I display the h:message components? I want to avoid going to the server. Thanks!</p> <p>This is the code of the calendars:</p> <pre><code>&lt;p:calendar id="dateFrom" value="#{reqAbsences.dateFrom}" navigator="true" showOn="button" required="true" pattern="dd/MM/yyyy" onSelectUpdate="dateTo dateFromVal hourInput timeFrom timeTo" selectListener="#{reqAbsences.handleDateFromSelect}" mindate="#{reqAbsences.today}" &gt; &lt;f:validator validatorId="DateValidator"/&gt; &lt;/p:calendar&gt; &lt;p:message id="dateFromVal" for="dateFrom" showSummary="false"/&gt; &lt;h:outputLabel value="#{text['global.toDate']}"/&gt; &lt;p:calendar id="dateTo" value="#{reqAbsences.dateTo}" navigator="true" showOn="button" onSelectUpdate="dateToVal" selectListener="#{reqAbsences.handleDateToSelect}" pattern="dd/MM/yyyy" required="true" mindate="#{reqAbsences.dateFrom}"&gt; &lt;f:validator validatorId="DateValidator"/&gt; &lt;/p:calendar&gt; &lt;p:message id="dateToVal" for="dateTo" showSummary="false"/&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload