Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, there is no "best" way. There's just the "right" way which depends on the concrete functional requirements. </p> <p>The normal approach to validate in JSF is to use one of the <a href="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/" rel="nofollow noreferrer">standard validators available via <code>&lt;f:validateXxx&gt;</code> tags</a>, or to <a href="https://stackoverflow.com/questions/6047866/how-to-perform-validation-in-jsf-how-to-create-a-custom-validator-in-jsf">create a class which implements the Validator interface</a> if the desired functionality is not available in the standard tags. An alternative would be to validate by JavaScript as you suggested yourself, but this completely defeats the <a href="https://stackoverflow.com/questions/4013410/jsf2-validation-clientside-or-serverside">robustness of server-side validation</a> because JavaScript code is under full control by the enduser and thus editable/spoofable/disablable by the enduser.</p> <p>In your particular case, you want to validate the input whether it matches a regular pattern. In that case, the <code>&lt;f:validateRegex&gt;</code> is thus the right tag for the job. </p> <p>As to the actual regular pattern, any number between 1 and 9 is in regex represented by <code>[1-9]</code> and any alphabetic character between A and Z is in regex represented by <code>[A-Z]</code> (case sensitive! if you intend to allow lowercase as well, use <code>[a-zA-Z]</code>). Any "zero or one occurrence" like as the last number is in regex represented by <code>(...)?</code> whereby the <code>...</code> is to be substituted with the actual pattern. The remainder, the characters <code>/</code> and <code>-</code> can be represented as-is as long as those are no special characters in regex such as <code>.</code>, <code>(</code>, etc, otherwise they needs to be escaped with <code>\</code>.</p> <p>So, all in all, this should do:</p> <pre><code>&lt;h:inputText&gt; &lt;f:validateRegex pattern="[1-9]/[A-Z]-[1-9]-[1-9]-[1-9](-[1-9])?" /&gt; &lt;/h:inputText&gt; </code></pre> <h3>See also:</h3> <ul> <li><a href="http://www.regular-expressions.info/" rel="nofollow noreferrer">Regex tutorial</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html" rel="nofollow noreferrer"><code>java.util.regex.Pattern</code> javadoc</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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