-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a textbox field where a user should enter an e-mail address. I want to validate the input. If I add a constraint within the XML (.zul) file, the validation works fine. If I create an instance of a SimpleConstraint and call email.setConstraint(SimpleCnstraint) it does not work (no matter what the user enters, the field is considered invalid and they get a message). Below are both the working example done within the zul file and the example done in code that I cannot get to work:
This works:
<textbox id="email" width="300px" constraint="no empty, /^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.]){2,6}$/: Please enter a valid e-mail address."/>
This does NOT work:
String emailValidation = "/^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.]){2,6}$/"; SimpleConstraint emailConstraint = new SimpleConstraint(emailValidation, "Please enter a valid e-mail address."); email.setConstraint(emailConstraint);
I've tried removing the leading and trailing "/" in the regular expression String used in the java code and still had no luck. The java code was located in the doFinally() method of the genericforwardcomposer.
any ideas?
<zk> <window apply="test.ConstraintTest"> <textbox id="email" width="300px" constraint="/^\w+@+?\.{2,3}$/"/> <textbox id="email2" width="300px"/> </window> </zk>
public class ConstraintTest extends SelectorComposer<Window> { @Wire Textbox email; @Wire Textbox email2; @Override public void doAfterCompose(Window comp) throws Exception { super.doAfterCompose(comp); email2.setConstraint(new SimpleConstraint("/^\\w+@+?\\.{2,3}$/")); } }
Strange... I tried exactly what you typed ("[email protected]") and it passes validation for me. Tomcat 7, JRE 7, ZK 6.0.1.
I'm pretty sure your regexp is wrong. I don't know why it passes validation, but if we look at the regexp closely, it doesn't seem to be correct.
The right-hand side of this regexp is this:
([0-9a-zA-Z]*[.]){2,6}$/
This means that the e-mail string must end with at least 2, and at most 6 times a string that looks like this:
something.
However, that is not a valid e-mail address. Because you require at least two suffix strings that have a dot in the end, I don't see how your regexp could ever validate an e-mail address correctly. I recommend that you look at this page where you can find some very good e-mail regexpressions: Comparing E-mail Address Validating Regular Expressions
hi @gekkio
thanks for your info ~~
the link Comparing E-mail Address Validating Regular Expressions seems great
related issue http://tracker.zkoss.org/browse/ZK-3017
Asked: 2012-01-28 22:20:17 +0800
Seen: 533 times
Last updated: Dec 10 '15