开发者

Validating ui:fragments

开发者 https://www.devze.com 2023-02-15 13:31 出处:网络
How can validate this code that user enter a valid ip address i want show message when user开发者_如何学C enter 0.0.0.0i can use a request scoped bean and register it for all inputtext in inputtadress

How can validate this code that user enter a valid ip address i want show message when user开发者_如何学C enter 0.0.0.0 i can use a request scoped bean and register it for all inputtext in inputtadress fragment and check 4 inputtext field but i want to know is there any other way .

     <ui:fragment >
                    <ui:include src="/misc/inputaddress.xhtml">
                        <ui:param name="id" value="ip" />
                        <ui:param name="value"
                                  value="#{externalDataStorageAdder.storage.inputAddr}"
                                  />

                    </ui:include>
                </ui:fragment>

and this inputaddres

<div
    style="border: 1px solid #c0c0c0; background-color: #ffffff; width: 162px;">
<h:inputText id="#{id}-field1"
    style="width: 25px; border: 0px; text-align: center; background-color: white; background-image: url('');"
    maxlength="3" value="#{value.field1}" onfocus="this.select();"
    onkeyup="number_only(this);">
    <f:validateLongRange minimum="0" maximum="255" />
</h:inputText> . <h:inputText id="#{id}-field2"
    style="width: 25px; border: 0px; text-align: center; background-color: white; background-image: url('');"
    maxlength="3" value="#{value.field2}" onfocus="this.select();"
    onkeyup="number_only(this);">
    <f:validateLongRange minimum="0" maximum="255" />
</h:inputText> . <h:inputText id="#{id}-field3"
    style="width: 25px; border: 0px; text-align: center; background-color: white; background-image: url('');"
    maxlength="3" value="#{value.field3}" onfocus="this.select();"
    onkeyup="number_only(this);">
    <f:validateLongRange minimum="0" maximum="255" />
</h:inputText> . <h:inputText id="#{id}-field4"
    style="width: 25px; border: 0px; text-align: center; background-color: white; background-image: url('');"
    maxlength="3" value="#{value.field4}" onfocus="this.select();"
    onkeyup="number_only(this);">
    <f:validateLongRange minimum="0" maximum="255" />
</h:inputText></div>


You can use validator , here is very nice article from BalusC


You can use your own validator like Joshi suggested (in fact, I used that BalusC article as well) or just check the ip-address in the action and post a message if "0.0.0.0" was entered.

What we normally do is to do a syntax check on individual fields in the validation phase and a more throrough semantic check (multiple fields in combination) in the invoke application phase.


If you don't want to check it in the bean you can do it with javascript. Call a javascript function each time a key is pressed in your input field and check in your function if all inputfields contain zero:

<h:inputText id="..." onkeypress="keyPressed();" .../>

and in your script:

function keyPressed() {  
 check for zero content
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消