admin管理员组

文章数量:1313822

I need to validate (check whether the number is less than 50000) a field on html site, field in inside some form.

<p:inputText id="xid" styleClass="span3"value="#{user.xd}" maxlength="5" required="true">
    // the validator does not work
    <f:validateLongRange maximum="50000" minimum="1"/>
    <p:clientValidator event="keyup"/>
</p:inputText>

I need to verify it during user's writing data. I also was checking jQuery code:

$(document).ready(function() {
        $("#form").validate({
            rules: {
                xid: {
                    required: true,
                    maxlength: 5,
                    min: 1,
                    max: 50000
                }
            },
            messages: {
                xid: {
                    required: "This is mandatory. Either generated or  manually entered!",
                    maxlength: "Max length is 5 digits",
                    max: "Max Value is 50000",
                    min: "Min is 1",
                    pattern: "Cannot contain characters nor starting with 0."
                }
            }
        });
});

The issue in the solution is the fact, that when JSF is applied the id's of forms are no so simple like "form"- names contains also ":" and JavaScript cannot handle them.

I need to validate (check whether the number is less than 50000) a field on html site, field in inside some form.

<p:inputText id="xid" styleClass="span3"value="#{user.xd}" maxlength="5" required="true">
    // the validator does not work
    <f:validateLongRange maximum="50000" minimum="1"/>
    <p:clientValidator event="keyup"/>
</p:inputText>

I need to verify it during user's writing data. I also was checking jQuery code:

$(document).ready(function() {
        $("#form").validate({
            rules: {
                xid: {
                    required: true,
                    maxlength: 5,
                    min: 1,
                    max: 50000
                }
            },
            messages: {
                xid: {
                    required: "This is mandatory. Either generated or  manually entered!",
                    maxlength: "Max length is 5 digits",
                    max: "Max Value is 50000",
                    min: "Min is 1",
                    pattern: "Cannot contain characters nor starting with 0."
                }
            }
        });
});

The issue in the solution is the fact, that when JSF is applied the id's of forms are no so simple like "form"- names contains also ":" and JavaScript cannot handle them.

Share Improve this question edited Jul 1, 2017 at 14:48 Aritz 31.7k16 gold badges147 silver badges227 bronze badges asked Apr 20, 2015 at 7:44 Tomasz WaszczykTomasz Waszczyk 3,1696 gold badges43 silver badges79 bronze badges 2
  • There are many choices for this question. The 'real time validation' concept does not exist. 'Client side validation' and 'Server side validation' are the proper terms. JSF by its own doesn't have client side validation capabilities, but you can play with the ajax events to send the input to the server each time a user performs a key up event. That's not as fast as the client side validation, but it's perfectly valid. As you need to validate the value on the server side (remember, server side validation is a MUST in the www) I suggest you to try it and let the client for later. – Aritz Commented Apr 20, 2015 at 7:55
  • Maybe you can use an inputmask for this? And what is wrong with the PrimeFaces clientValidator? – Kukeltje Commented Apr 20, 2015 at 7:58
Add a ment  | 

1 Answer 1

Reset to default 9

Forget about adding extra jQuery code and just go with the JSF validator along with the PrimeFaces client validator tag. You'll see how the input field turns red for values greater than 50000. I personally tend to use the change event instead of keyUp, I consider it to be more user friendly:

<html xmlns="http://www.w3/1999/xhtml"
    xmlns:h="http://xmlns.jcp/jsf/html"
    xmlns:f="http://xmlns.jcp/jsf/core"
    xmlns:p="http://primefaces/ui"
    xmlns:p="http://java.sun./jsf/posite/p"
    xmlns:ui="http://java.sun./jsf/facelets">
<h:head />
<h:body>
    <h:form>
        <p:messages autoUpdate="true" />
        <p:inputText value="#{val}" maxlength="5" required="true">
            <f:validateLongRange maximum="50000" minimum="1" />
            <p:clientValidator event="change" />
        </p:inputText>
        <p:mandButton value="Check" />
    </h:form>
</h:body>
</html>

Don't forget to enable the client side validation in your web.xml:

<context-param>
    <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
    <param-value>true</param-value>
</context-param>

See also:

  • Primefaces clientValidator message not displayed

本文标签: javascriptClient validation in PrimefacesStack Overflow