user1495475
user1495475

Reputation: 1057

Validate and correct invalid emailid formats in C#

Is there a way we can validate and correct invalid format emailids in C#.I got a function which can only validate but not correction.Some emailds like "[email protected]." can be corrected.I`m fetching all emailids from database and sending them a mail,if I just remove invalid emailids,the person may loose info,so instead of removing I thought of correcting the mailid and send him the mail. Is there a way?Or a function to do this.??? Thanks in advance.

Upvotes: 2

Views: 368

Answers (3)

danish
danish

Reputation: 5600

No. There is no way to do this. You may have a built-in guess system that will take care of common mistakes though.

For instance, if I type my email id as [email protected], you may change it to [email protected]. This still does not guarantee that the email is is now correct.

Assume I had an email id as [email protected] and intentionally I typed in [email protected]. Now, there is no way you can correct it. With the same intention, if I type [email protected], your code might make it correct email id as [email protected] which still is incorrect.

Essentially what you are looking for is called client side validation. What ever front end you have, place validation that check if email address is correct as per syntax. For verifying if the user has given his real email, send a mail to the given address with activation link and ask them to click on it if they want to use applciation.

Edit:

If you need to just format the emails in database, you can check for common mistakes using queries/external executable. These will validate the data against a valid format which then, can be changed. What are the options you have, technology wise, for doing this?

Upvotes: 0

Aharon Manne
Aharon Manne

Reputation: 712

If you have the email address as a string, then you can manipulate the string. In your example, that would be removal of the trailing period. Other than this simple example, I suggest that you think long and hard about how useful this will be. What is the context? Can you pass the mail address back to a user to get the correct address, as opposed to your best guess?
Adding code will clarify your question. From your question, I don't know why you assume you can only validate, as opposed to correcting the mail address string.

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37576

You could check wheather the Mail domain exists for example like this, you can check if the Email ends with an unvalid char like "." or "," and remove this if found but you can not really "correct" wrong Emails by trying to change each char and check if the Email exists or not, and its not desired sicne you would find probably for each change you make an exissting Email adress which is not the one you really wish to reach.

Upvotes: 0

Related Questions