wood burning stoves 2.0*
The moose likes JSF and the fly likes Validating RegEx for email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Validating RegEx for email" Watch "Validating RegEx for email" New topic
Author

Validating RegEx for email

Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Hello everybody,

I am trying to add an email validator to my xhtml page, but I get this error:

<f:validateRegEx> Tag Library supports namespace: http://java.sun.com/jsf/core, but no tag was defined for name: validateRegEx

This is my code:

<h:inputText tabindex="7" styleClass="input" id="email" value="#{user.email}"
required="true" validatorMessage="Invalid email!">
<a4j:support id="emailRenderer" event="onblur"
reRender="emailPanel, errorPanel"
ajaxSingle="true"/>
<f:validateRegEx pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*(com|net|org|edu)" />
</h:inputText>
<p:message for="email" />


...

code header:

<html xml:lang="en" lang="eng" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">

What could be going wrong?

Thanks,

Matthew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Hi Matthew, welcome to the Ranch!

This problem is a good thing, since it provides you with an opportunity to change your mind about using a regex to validate e-mail addresses. It really isn't a good idea and your sample regex shows why -- it will declare many perfectly good e-mail addresses to be invalid. So just don't do it.
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Paul Clapham wrote:Hi Matthew, welcome to the Ranch!

This problem is a good thing, since it provides you with an opportunity to change your mind about using a regex to validate e-mail addresses. It really isn't a good idea and your sample regex shows why -- it will declare many perfectly good e-mail addresses to be invalid. So just don't do it.


Well then what should I do?
But actually you see I only want to accept edu/com/net/... email addresses. That's the point.
Maybe I oughta leave the Ranch/plantation.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

What should you do? I would suggest just not validating e-mail addresses. After all if people want to get e-mails from your system they will take some care in typing them correctly. Or if you don't feel comfortable with that, I have seen sites which ask you to type your e-mail address twice, in two separate fields. Presumably the site complains if the two fields don't contain the same data.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Matthew Cserhati wrote:But actually you see I only want to accept edu/com/net/... email addresses. That's the point.


Really? That strikes me as a very strange requirement. Is it possible for you to go into why that came about?
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Paul Clapham wrote:What should you do? I would suggest just not validating e-mail addresses. After all if people want to get e-mails from your system they will take some care in typing them correctly. Or if you don't feel comfortable with that, I have seen sites which ask you to type your e-mail address twice, in two separate fields. Presumably the site complains if the two fields don't contain the same data.


Okay but we don't want people to register from just anywahere, like with yahoo or gmail accounts. They have to be from the research community, hence we restrict email addresses to only net, edu or com.

There are tons of other websites which deal with email validation, I mean I've just googled them but I can't solve this special problem.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
OK, so maybe email validation is in order, but not via a regexp. Actually, a regexp might still be used, but only a very loose one that only catches obvious typos. That could be the first step. A pattern I have used in the past was "^\\S+@([-\\w]+\\.){1,4}[a-z]{2,6}$", but these days the last part can be more than 6 characters. And be sure to remove leading and trailing white space before applying this pattern.

The deeper problem is that you say you don't want Yahoo/Hotmail/Gmail addresses, but you do want .com in general - which all those are.

If this was my problem I might start with a whitelist and a blacklist. The whitelist might contain ".edu", ".ac.uk", ".ac.il" etc. - addresses that you know are welcome. The blacklist would contain "gmail.com", "googlemail.com", "yahoo.com", "hotmail.com" etc. - which you know are not welcome. You would iterate through both lists, and use String.endsWith to decide what to do about them.

But all the rest -".com", ".net", ".org" and most country domains- don't give you much to work with. So you need to decide how you want to handle those.


Ping & DNS - my free Android networking tools app
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Ulf Dittmer wrote:OK, so maybe email validation is in order, but not via a regexp. Actually, a regexp might still be used, but only a very loose one tat only catch obvious typos. That could be the first step. A pattern I have used in the past was "^\\S+@([-\\w]+\\.){1,4}[a-z]{2,6}$", but these days the last part can be more than 6 characters. And be sure to remove leading and trailing white space before applying this pattern.

The deeper problem is that you say you don't want Yahoo/Hotmail/Gmail addresses, but you do want .com in general - which all those are.

If this was my problem I might start with a whitelist and a blacklist. The whitelist might contain ".edu", ".ac.uk", ".ac.il" etc. - addresses that you know are welcome. The blacklist would contain "gmail.com", "googlemail.com", "yahoo.com", "hotmail.com" etc. - which you know are not welcome. You would iterate through both lists, and use String.endsWith to decide what to do about them.

But all the rest -".com", ".net", ".org" and most country domains- don't give you much to work with. So you need to decide how you want to handle those.


Thank you.
But one of my technical issues is that jsf seemingly doesn;t recognize Regexp as a validator class. It recognizes Length, DoubleRange, and LongRange, but not Regex.
Could you help me with that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
I know just about nothing about JSF. But it seems clear that this validation is beyond whatever built-in facilities JSF has. But I'm certain that JSF has a way of hooking a validation class of yours into it; a search for jsf custom validation class returns some promising results.
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Ulf Dittmer wrote:I know just about nothing about JSF. But it seems clear that this validation is beyond whatever built-in facilities JSF has. But I'm certain that JSF has a way of hooking a validation class of yours into it; a search for jsf custom validation class returns some promising results.


yo
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

JSF regex validation is not available until JSF version 2. That may be your problem.

A user-written custom validator can be used in JSF version 1.

Alternatively, if you're using a third-party extension tagset such as PrimeFaces, check their documentation. Some of the extensions actually include a specific custom JSF tag for things like email validation.


Customer surveys are for companies who didn't pay proper attention to begin with.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I went to a website about a month ago which appeared to want to make sure I was part of the academic community. And it was apparently doing that by using my IP address to see if it was an IP address belonging to a university somewhere.

I say "appeared to" and "apparently" because it was a Russian journal and my Russian isn't that great and I didn't care much if I was allowed access or not. To do that you would have to build a database of IP addresses which you wanted to grant access to, and my guess is that you probably won't want to do that. But it is an alternate approach which might be worth considering.
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
Paul Clapham wrote:I went to a website about a month ago which appeared to want to make sure I was part of the academic community. And it was apparently doing that by using my IP address to see if it was an IP address belonging to a university somewhere.

I say "appeared to" and "apparently" because it was a Russian journal and my Russian isn't that great and I didn't care much if I was allowed access or not. To do that you would have to build a database of IP addresses which you wanted to grant access to, and my guess is that you probably won't want to do that. But it is an alternate approach which might be worth considering.


Hello there eveybody!

Thanks to your help I solved the problem. I used a custom validator. However, just one note that in the online tutorials and help pages you have to use @FacesValidator annotation.

This wasn't working for me so I used this:

@Name("emailAddressValidator")
@Validator
@BypassInterceptors

Matthew
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

JSF can actually get validation from several sources.

In JSF1, you had to explicitly define JSF validators in faces-config.xml, but in JSF2, the validator annotation was added. JSF1 didn't support annotations. Although faces-config will still override an annotation, since that makes code reuse easier.

JSF can also be wired to work with the JSR-defined validator mechanism that's now supported by ORM systems. So if you have an ORM object model object that's referenced by an EL expression on a JSF View, that ORM's properties validators can be "borrowed" by JSF instead of requiring a second set of validation definitions just for JSF.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating RegEx for email