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.