Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Validate email address from GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jack Vale, May 4, 2015.

  1. Jack Vale

    Jack Vale

    Joined:
    Apr 7, 2015
    Posts:
    10
    How do I check if the user enters the correct format of the email address in the GUI they typed in?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
  3. Archie888

    Archie888

    Joined:
    May 17, 2014
    Posts:
    41
    Here is a class that I grabbed from somewhere:

    Code (CSharp):
    1. /*
    2. * File: TestEmail.cs
    3. * Author: Mykola Dobrochynskyy
    4. * Version 1.0
    5. * Created date:  07.01.2008
    6. * Last upadte date:
    7. * Project: Common Utils
    8. * Description: Implements the static class, that could be used to check an E-Mail address.
    9. * -----------------------------------------------------------------------------
    10. * You are eligible to use this code for you own purpouse assuming that this
    11. * code and information in it is provided "AS IS" without warranty of any kind
    12. * expressed or implied.
    13. * ------------------------------------------------------------------------------
    14. */
    15. using System;
    16. using System.Collections.Generic;
    17. using System.Text;
    18. using System.Text.RegularExpressions;
    19.  
    20. //namespace Dobro.Text.RegularExpressions
    21. //{
    22.     /// <summary>
    23.     /// Tests an E-Mail address.
    24.     /// </summary>
    25.     public static class TestEmail
    26.     {
    27.         /// <summary>
    28.         /// Regular expression, which is used to validate an E-Mail address.
    29.         /// </summary>
    30.         public const string MatchEmailPattern =
    31.             @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
    32.                 + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
    33.                 + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
    34.                 + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
    35.        
    36.        
    37.         /// <summary>
    38.         /// Checks whether the given Email-Parameter is a valid E-Mail address.
    39.         /// </summary>
    40.         /// <param name="email">Parameter-string that contains an E-Mail address.</param>
    41.         /// <returns>True, wenn Parameter-string is not null and contains a valid E-Mail address;
    42.         /// otherwise false.</returns>
    43.         public static bool IsEmail(string email)
    44.         {
    45.             if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
    46.             else return false;
    47.         }
    48.     }
    49. //}
     
    ali12000, Spicyruby and Novack like this.
  4. Jack Vale

    Jack Vale

    Joined:
    Apr 7, 2015
    Posts:
    10
    Thanks, but how do I grab the text from GUI text box and compare with the regular expression?