input field email validation

Hello all I am using an input field and have the emailaddress validation which works somewhat but I am wondering how to make sure the entered address has an @.

Thank you

Using regular expressions. Hopefully this link will help.

1 Like
bool hasAt = emailAddress.IndexOf('@') > 0;
2 Likes

Easiest would be just a call to the Contains method (or similar ones) of a string, but that’s the dirty and wrong way.

The proper way are regular expressions. The reason being that emails have actually some kind of specification for their format and you can determine (in most cases) whether it’s a correctly formatted email-address or not.
There are different RegEx’ out there, some are quite complex due to the attempt to cover every possible format and thus are pretty long, yet they sometimes do still not cover everything (edge cases).
But there are quite good ones that have a moderate length & complexity.

use Regex!
Example to a sample of Regex that I might use to test: regex101: build, test, and debug regex
How to search strings - C# | Microsoft Learn

EDIT: As I was typing this the other answers do exactly what you want, if all you want to do is check for an @ symbol. Depends how much verification you want.

Hello All thank you for the replies on quick question is how do I keep focus on the input field until this @ sign is verified.

You can use the Select method to focus the input field

if (inputField.text.IndexOf('@') <= 0)
{
    inputField.Select();
}

https://docs.unity3d.com/ScriptReference/UI.Selectable.Select.html

inputfield does not seem to work

but I have to use
zipCodeInputField.Select ();
zipCodeInputField.ActivateInputField ();

but then I can select on other things still

don’t use Regex!

Especially not if checking for the presence of an @ symbol will do. Almost all the regexes are written by people that don’t read the RFC docs and merely think they understand the format of an email address.

The few regexes that were written to attempt to follow the actual rules are pretty easy to spot, they are most often well over 100+ characters long and usually come with recommendations not to use them because they won’t be 100%.

In a related thread, I followed some links that lead me to some person or persons that suggested you can use this to validate an email: MailAddress Constructor (System.Net.Mail) | Microsoft Learn

  • Using the format exception, I believe, to determine if it was valid. Just thought I’d share :slight_smile:

Guessing that MailAddress considers something like “postmaster ” as valid (haven’t checked)

Ok, now that you have provided a don’t, what about a do?

1 Like

You are four years late to the party. Don’t spam old threads, it’s against forum rules.

Here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Who are you, telling me what is spam and what not? This 4 year old thread still hasn’t a proper answer. And it is to criticize when an answer tells you what not to do, but has no proper solution mentioned.

Your 12.000 post could also be an indicator for a lot of spaming…

2 Likes

That escalated quickly.

I would probably go with what @methos5k suggested (https://msdn.microsoft.com/en-us/library/591bk9e8(v=vs.110).aspx) and just checking for spaces to avoid what @nat42 pointed out since email addresses cannot have spaces in them.

However, to be fair - the original question was

For which several answers have been provided. The discussion has since spiraled into email validation which is notoriously difficult.

I am merely trying to help you to understand the forum rules, as outlined here:

https://discussions.unity.com/t/757848

Have a wonderful day!

1 Like

The issue is raising a necro thread from the dead to ask a question that is already answered in the same thread is just irritating for everyone. Plus, nat42 hasn’t logged into the forum this year, so he’s probably not going to respond to your post anytime soon.

I use a regex, which is really easy if you’ve done those before. I don’t see what nat42 is stressed out about, and his reasoning is ridiculous. Unless you are writing your own SMTP server, your use case most likely does not require an email address validator to check that the provided string matches every rule ever in every RFC.

MailAddress is also not a bad idea most likely, but I haven’t used it.

Yea i also use RegEx since years without problems. I know how to validate eMails with that. That’s why the point was: When someone says don’t do this, but does not provide an alternative, that message is quite pointless.

But ok, i can always go over to stack exchange searching for a proper answer why not to use regex.

MailAdress works, but i have no idea if it should be used instead of regex.

Ohh, i see. 12.000 posts teaching people forum rules. Very helpful.

On the other hand, proper email verification never gets old, so it depends on the topic if a thread is old and useless. A proper answer to this is still helpful. But may be those cases are not written down in your book of rules.

1 Like

Hi, you can use this gits code for E-mail validation with regex. It is really work :slight_smile:

or

// Step 1: Import necessary namespace for regex operations
using System.Text.RegularExpressions;

// Step 2: Define a constant pattern for email matching
public class EmailValidator
{
    // This pattern is designed to match standard email formats, including those with domain and subdomain names.
    // It's structured to validate both the local part and the domain part of the email address.
    public const string EmailPattern =
        @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + // Local part
        @"((([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])\." + // IP address
        @"([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}|" + // IP address continuation
        @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$"; // Domain name

    // Step 3: Define a method to validate an email address using the regex pattern defined above
    public static bool ValidateEmail(string email)
    {
        // Check if the email is not null or empty to proceed with regex matching
        if (!string.IsNullOrEmpty(email))
        {
            return Regex.IsMatch(email, EmailPattern);
        }
        else
        {
            // Return false if the email is null, meaning it's not a valid email address
            return false;
        }
    }
}