0

Regular Expression validation issue using SimpleConstraint

asked 2012-01-28 22:20:17 +0800

eclement gravatar image eclement
21

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?

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2012-05-16 01:39:29 +0800

samchuang gravatar image samchuang
4084 4
hi here is my test, it works fine
<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}$/"));
	}
}
link publish delete flag offensive edit

answered 2012-05-30 19:24:36 +0800

eclement gravatar image eclement
21

Agreed, it works fine when the regular expression is short, but when it's very long as in my example, it does not.

link publish delete flag offensive edit

answered 2012-05-31 00:40:03 +0800

samchuang gravatar image samchuang
4084 4

that's strange, by the way, what do I need to type to pass your constraint ?

link publish delete flag offensive edit

answered 2012-05-31 11:49:58 +0800

eclement gravatar image eclement
21

Any valid e-mail address. :-)

link publish delete flag offensive edit

answered 2012-06-01 01:06:42 +0800

samchuang gravatar image samchuang
4084 4

hi

I tried "[email protected]", but won't pass your Constraint

link publish delete flag offensive edit

answered 2012-06-01 12:22:29 +0800

eclement gravatar image eclement
21

Strange... I tried exactly what you typed ("[email protected]") and it passes validation for me. Tomcat 7, JRE 7, ZK 6.0.1.

link publish delete flag offensive edit

answered 2012-06-01 12:52:04 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

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.
(note the dot at the end).
That doesn't make any sense with e-mail addresses. For example:
That e-mail address has one such string ("yahoo."), but not two because the part "com" does not end with a dot. The regexp passes if you add a dot to the end so that "com." becomes another match:

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

link publish delete flag offensive edit

answered 2012-06-04 00:59:13 +0800

samchuang gravatar image samchuang
4084 4

updated 2012-06-04 01:02:38 +0800

hi @gekkio

thanks for your info ~~

the link Comparing E-mail Address Validating Regular Expressions seems great

link publish delete flag offensive edit

answered 2015-12-10 07:51:31 +0800

cor3000 gravatar image cor3000
6280 2 7

related issue http://tracker.zkoss.org/browse/ZK-3017

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-01-28 22:20:17 +0800

Seen: 533 times

Last updated: Dec 10 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More