Link to home
Start Free TrialLog in
Avatar of EmailSurfer
EmailSurfer

asked on

Validating string email?

Hello,

I am trying to require a basic validation rule to check an email address.

My plan was to use indexOf to test for the @ symbol.

I also wanted to check there was somewhere after the @ symbol an (.)

Does anyone know if I could use the indexOf method to test for the . after the @?

Thank you
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Best to validate with regex

http://www.codetoad.com/asp_email_reg_exp.asp
In Java those escape character \ need to be doubled
Avatar of EmailSurfer
EmailSurfer

ASKER

Thanks

Can we use Regex in java servlet classes?
Yes, sure. You need to import the correct package.
Thanks

Each of these links seem to recommend a different pattern.

I thought as the email address format can take many forms.

If I just tested these was a single @ and atleast 1 (.) symbol after the @. This was really all I could test for.

Does this seem correct?
Patterns can be different and there are different ways to achieve the same thing
Apparently, the expression that validates email addresses per RFC 822 is 6343 characters long ;-)

http://www.regular-expressions.info/email.html
Try the one posted on Sun's web-site, for a start.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Simplest algorithm:

Use java built in validator, then apply the folloiwng:

Check indexOf(@) > -1 and indexOf(.) > indexOf(@)
Split(@) - you should have an array length 2
spli(.) - you should have an array AT LEAST length 2

colr__
:-)