2

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?

Gordons
  • 324
  • 5
  • 17
  • Possible duplicate of [Codename One - RegexConstraint to check a valid phone number](https://stackoverflow.com/questions/48481888/codename-one-regexconstraint-to-check-a-valid-phone-number) – Dsenese1 Jan 14 '19 at 11:29
  • @Dsenese1 it's no duplicate, both concepts are not same. – Gordons Jan 14 '19 at 11:33

2 Answers2

3

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");
       }
Gordons
  • 324
  • 5
  • 17
1

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.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65