-3

I'm taking a C# intro class so we're not using anything complicated, just methods and loops. I've been trying to research and find more information on how to use string methods for this email check assignment we're supposed to do. But every time I look for anything, I get regex which I'm not allowed to use.

I was just hoping if anyone familiar with string methods or just simply experienced in coding, could help point me in the right direction of where to start or give an example of a string method that checks if a character is after a certain spot or if a string character is actually there.

I was thinking of using the IndexOf method but the professor didn't show us too well of how to implement it in a method and return it back to the main method.

Would that be a good place to start? I know that if I use IndexOf to check if an "@" sign is or is not in the hardcoded email address, I would get either the index number of where it is at or a -1 if it is false. How would that be used in a method to return in the main method?

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • Welcome! Start by researching; experimenting; debugging and finally submitting your homework. Wishing you well. [ask] –  Oct 16 '16 at 15:50
  • Read the string as a [char array](https://msdn.microsoft.com/en-us/library/2c7h58e5(v=vs.110).aspx), check as per the [email grammar](https://tools.ietf.org/html/rfc5322#section-3.4.1), you may need a [backtracking](https://en.wikipedia.org/wiki/Backtracking) [algorithm](https://en.wikibooks.org/wiki/Algorithms/Backtracking). For more info on parsing see the [Dragon Book](https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools). Algo get a [List of Emails for Unit Tests](http://stackoverflow.com/questions/7641019/list-of-mock-valid-invalid-email-addresses-for-unit-tests). – Theraot Oct 16 '16 at 15:52
  • Thanks for the help! I think I'll give the char array a shot and see how it goes. – Sydney Taing Oct 16 '16 at 15:56
  • @SydneyTaing Also note that email validation on UI only has value preventing common mistakes and evidently wrong input. In practice a mail should be sent to verify both its correctly and its existence. Edit: In fact, some solution will only check for the presence of "@" with some text before and after. I don't know how good the validation must be for your case. – Theraot Oct 16 '16 at 16:01

1 Answers1

0

IndexOf will return the position of the element of it exists, or -1 if doesn't.

I would suggest you do a String.IsNullOrEmpty(value) in an if statement, return false and a friendly message if the value is not set. Next, inside your if statement, check if you have an "@" using IndexOf, then do a value.LastIndexOf("."). If you have an "@" and it's last index value is less than the last index of the "." ...then the email is valid.

You can also check if the count of the "@" character is one if it does exist. Use a for loop and count value or use value.Toarray().where(x=>x == "@") > 1.

Theraot
  • 31,890
  • 5
  • 57
  • 86
mahlatse
  • 1,322
  • 12
  • 24
  • On a contrary, there are more checks to do. an email address like `name@@domain@.` would pass your test, but it's not valid. 1. `Split` your `email` using the `@` character and then count the parts. They must be exactly 2 to continue the checks. 2. Make sure that each part doesn't start or end with a dot. Use `string.StartsWith` and `string.EndsWith` 3. Make sure that there are no two dots following each other. `IndexOf("..")` must be `-1`. – RealSollyM Nov 22 '16 at 12:53