Codename One Email validation

Viewed 265

I have been trying to create a simple Email validation method, that shows the existence of a username@domainname.com, to no success. I tried this:

public boolean validateEmailAddress(String emailAddress) {
RE pattern = new RE("^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$");
return pattern.match(emailAddress);
}

I imported RE from

com.codename1.util.regex.RE

Each time I test it, I get the Error message:

Exception: com.codename1.util.regex.RESyntaxException - Syntax error: Missing operand to closure

What is the best way to achieve this?

2 Answers

I discovered a very simple way to do this validation. I didn't know it existed:

TextArea userName = new TextArea(); 
Validator validator = new Validator();
validator.addConstraint(userName, RegexConstraint.validEmail());

If you would like to provide your own Regular expressions for Validation, do the following:

RegexConstraint emailConstraint = new RegexConstraint("^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$", "Invalid Email Address");
validator.addConstraint(userName, emailConstraint);

Then to be sure that the validation is successful, perform the following check :

if (!validator.isValid()) {
         ToastBar.showErrorMessage("You have to make all corrections");
       }

In RegexConstraint we use this for email validation:

^([a-zA-Z0-9.!#$%&'*+/=?^`{|}~]|-|_)+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$

I'm pretty awful with regex so I might be completely off... But it seems to me that the ( and [ are reversed in your version.

Related