test.zul:
<?page title="" contentType="text/html;charset=UTF-8"?> <zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.zkoss.org/2005/zul"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul">
<window id="testWin" border="none" apply="test.test19">
邮箱:<textbox id="textbox"/>
</window>
</zk>
test19.java:
package test; import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Textbox; public class test19 extends GenericAutowireComposer<Component>{ private static final long serialVersionUID = 1L; private Textbox textbox; @Override
public void doAfterCompose(Component comp) throws Exception {
// TODO Auto-generated method stub super.doAfterCompose(comp); textbox.setConstraint(new Constraint() { @Override
public void validate(Component comp, Object obj)
throws WrongValueException {
if (obj == null || obj.toString().trim().length() == 0) {
throw new WrongValueException(comp, "邮箱不能为空!");
}else {
String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
boolean result = obj.toString().matches(regex);
if (result == false) {
throw new WrongValueException(comp, "邮箱格式有误!");
}
}
}
});
}
}
效果: