JSF server-side validation user input

Source: Internet
Author: User

Server-side validation user input data steps

1 inserting a Data control on an HTML page

?
1 2 3 4 "10" value="#{commodityBean.foradd.name}"                         id="input1">                         <f:validator validatorId="input1Validator" />                     for="input1">

This is to verify the value of the Inputtext control with Input1validator. The result is then returned in message form. The logical end of true authentication is performed on the server side.

2 write in Face-config.xml inside the Web-info folder

?
1 2 3 4 5 6 7 8 <validator>  <validator-id>  input1Validator </validator-id>  <validator-class com.fujitsu.softbg.zl.input1Validator </validator-class</validator>

Notifies the server to brake to find the Input1validator.java file under the Com.fujitsu.softbg.zl folder.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.fujitsu.softbg.zl;import java.util.regex.Matcher; import java.util.regex.Pattern;import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException;public class input1Validator implements Validator {    @Override     public void validate(FacesContext arg0, UIComponent arg1, Object arg2)             throws ValidatorException {         // TODO Auto-generated method stub         String inputvalue=arg2.toString();            String regEx="[0-9.]+";//表示一个或多个数字            Pattern p=Pattern.compile(regEx); //编译成模式           Matcher m=p.matcher(inputvalue); //创建一个匹配器           boolean rs=m.matches();           if(!rs){               FacesMessage message = new FacesMessage(                     FacesMessage.SEVERITY_ERROR, "not a vaild number",                     "not a vaild number");             throw new ValidatorException(message);           }                 } }

This verifies that the data entered by the user is 0 to 9 and a decimal point. Returns a prompt message if it is not compliant. A user-entered character can also be validated in a logical expression on the server side, just like JavaScript.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.