none
User input in C# RRS feed

  • Question

  • Hello,

    I am new to C# and I'm trying to make Sign in and Sign up app.

    I tried to make that when user writes in email textbox that if it doesn't have @gmail.com or @hotmail.com, program pops up a message saying "Please enter a valid email"

    This is how I did it:

    if (txtEmail.Text ...) // here I didn't know how to make script { MessageBox.Show ("Please enter a valid email form"); }

    Best regards,

    Gliske

    Wednesday, December 26, 2018 2:29 PM

Answers

  • There are a lot of examples you will find to validate the email as input from user. See the following post:

    https://stackoverflow.com/a/1374644/1875256


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Wednesday, December 26, 2018 2:45 PM
    • Marked as answer by Gliske Wednesday, December 26, 2018 7:03 PM
    Wednesday, December 26, 2018 2:44 PM
  • If you want to validate only @hotmail.com and @gmail.com you can use string.EndsWith method. 

    if (!textbox1.Text.ToLower().EndsWith("@gmail.com") && !textbox1.Text.ToLower().EndsWith("@hotmail.com"))
        MessageBox.Show("Email is not valid");


    • Edited by Petr B Wednesday, December 26, 2018 3:44 PM
    • Marked as answer by Gliske Wednesday, December 26, 2018 7:03 PM
    Wednesday, December 26, 2018 3:44 PM

All replies

  • There are a lot of examples you will find to validate the email as input from user. See the following post:

    https://stackoverflow.com/a/1374644/1875256


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Wednesday, December 26, 2018 2:45 PM
    • Marked as answer by Gliske Wednesday, December 26, 2018 7:03 PM
    Wednesday, December 26, 2018 2:44 PM
  • If you want to validate only @hotmail.com and @gmail.com you can use string.EndsWith method. 

    if (!textbox1.Text.ToLower().EndsWith("@gmail.com") && !textbox1.Text.ToLower().EndsWith("@hotmail.com"))
        MessageBox.Show("Email is not valid");


    • Edited by Petr B Wednesday, December 26, 2018 3:44 PM
    • Marked as answer by Gliske Wednesday, December 26, 2018 7:03 PM
    Wednesday, December 26, 2018 3:44 PM
  • Odd that you require either of those domains. This sounds like a homework assignment to me so I'm curious what you've learned about at this point - string comparison, classes, etc?

    If you have just learned about string comparison then EndsWith (case insensitive) would be sufficient. However if you have learned about classes then I'd use MailAddress instead. Not only will it validate the string is a valid email but it also gives you the Host property containing the value you want. A simple comparison (case insensitive) would then verify the domain is valid.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, December 26, 2018 4:07 PM
    Moderator
  • What if I want every email that is valid to use? How do I get the source or form of that kind of mail?
    Wednesday, December 26, 2018 7:07 PM