Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
AbeesMuhammed
Posts: 49
Registered: ‎05-20-2009
My Device: Not Specified

How to Validate an Email Address in BlackBerry Applications

Hi,

 

I have a sample application for testing Given Email Id is Valid or not.In this Application I have a Basic EditField with Style as BasicEditField.FILTER_EMAIL.Im checking isdatavalid() method of the basic editfield is true or not while clicking on "Validate"  menu Item.My issue is that isdatavalid() always returns true if the editfieldcontains any string,even if it is not a valid EmailId, How can i validate user Entered EmailId is valid or not?.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

Use an EmailAddressEditField rather than a BasicEditField, it is a very useful field because it helps the user's type email addresses in - most Blackberry users are used to this Fields actions and routinely hit the space bar rather than the '.' or @.

 

If you want to use a BasicEditField, then I think the EmailAddressTextFilter is a good filter to use, for example:

 

BasicEditField bef = new BasicEditField("Email: ", null);

bef.setFilter(new EmailAddressTextFilter());

 

I find using Filters in this way gives better results than using what I think should be the equivalent Style,  But use EmailAddressEditField  if you can.

Please use plain text.
Developer
AbeesMuhammed
Posts: 49
Registered: ‎05-20-2009
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

its not worked.
Please use plain text.
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

Use EmailAddressEditField

 


Press the kudos button to thank the user who helped you.

If your problem was get solved then please mark the thread as "Accepted solution".

 

 

 

Deepesh Gupta
Please use plain text.
Developer
AbeesMuhammed
Posts: 49
Registered: ‎05-20-2009
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

i used EmailAddressEditField but it didn't work
Please use plain text.
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

if you talking about spaces then i read somewhere that it is  in fact valid to have a space in an email address.

 


Press the kudos button to thank the user who helped you.

If your problem was get solved then please mark the thread as "Accepted solution".

 

Deepesh Gupta
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

"i used EmailAddressEditField but it didn't work"

 

It would be useful to know exactly what did not work.  Can you tell us that?

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to Validate an Email Address in BlackBerry Applications

There are two kinds of validation: whether the string "looks like" an email address and whether it is an actual address. The latter is extremely difficult and about the only reliable way is to send an email to that address and check that it gets delivered.

 

I assume that you're only trying to accomplish the first kind of validation. Like others have said here, EmailAddressEditField is a good place to start. That only does a limited amount of checking, though--you can assume that the field only has characters that are valid in an email address. However, EmailAddressEditField doesn't override isDataValid(). You can do that yourself by extending EmailAddressEditField, although be aware that deciding whether an address looks valid is itself non-trivial.

 

Here's a very simple attempt:

 

public class ValidatingEmailAddressEditField extends EmailAddressEditField { // ... /** * Validates an email address. Checks that there is an "@" * in the field and that the address ends with a host that * has a "." with at least two other characters after it and * no ".." in it. More complex logic might do better. */ public boolean isDataValid() { String address = getText(); int at = address.indexOf("@"); int len = address.length(); if (at <= 0 || at > len - 6) return false; String host = address.substring(at + 1, len); len = host.length(); if (host.indexOf("..") >= 0) return false; int dot = host.lastIndexOf("."); return (dot > 0 && dot <= len - 3); } }

 

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.