How to use Regular Expressions (regex) in GWT?

2,424 views
Skip to first unread message

omsrobert

unread,
Nov 17, 2008, 2:06:39 PM11/17/08
to Google Web Toolkit
How do you use regular expressions in GWT? Can someone post a small
code sample? I'm looking to validate an e-mail address against a RFC.

alex.d

unread,
Nov 18, 2008, 2:22:40 AM11/18/08
to Google Web Toolkit
No GWT magic here - just POJ:

if(!tbEmail.getText().matches("[a-z0-9!#$%&'*+/=?^_`{|}~-]+...bla bla
bla...")) {
Window.alert("Invalid email");

Sumit Chandel

unread,
Nov 19, 2008, 8:56:11 PM11/19/08
to Google-We...@googlegroups.com
Hi omsrobert,

As a side note, it's worth mentioning that regular expressions used in GWT will be evaluated in the JavaScript world and not the Java world. There are slight syntactic differences between the two, so while looking up references for regular expressions, you should consult resources for JavaScript regex rather than Java.

Hope that helps,
-Sumit Chandel

Reinier Zwitserloot

unread,
Nov 20, 2008, 6:53:57 AM11/20/08
to Google Web Toolkit
Validating an email address in a regexp is somewhat impractical.

The problem is that emails are far more lenient than you might think.

However, if you're willing to ignore the standard and go with what
most people consider a valid email address...

try something like:

theEmailAddress.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]
{2,}$");

I've read the almost complete one at one point or other, and it was
half a screen long. I can't find it, unfortunately.

Personally, I'd check for an @, a . in the server name, no whitespace,
and other than that, accept it.
Reply all
Reply to author
Forward
0 new messages