Free Web Development Online Tutorials, Learn to Code
C# String validation example, check if string input is in expected format using c#

C# String Validation

Let’s learn how we can validate string using C#, while capturing user data on web page we often receive different type of data, but before we can submit data into database we need to validate the type of data we are getting.

Here we learn C# string validation, validate data like email id, name, number, alphanumeric data, date etc in C# code.

string validation c# example

  • C# Email validation syntax: we can validate if a string is valid email id, there are two different way to validate it.

    public static bool isValidEmailId(string email)
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(email))
                isValid = false;
            else
            {
                try
                {
                    MailAddress m = new MailAddress(email);
                    isValid = true;
                }
                catch (FormatException fx)
                {
                    isValid = false;
                }
            }
            return isValid;
        }
    

    We also can verify email using regular expression in C# like example below.

    bool isEmail = Regex.IsMatch(emailString, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
    
  • Sometimes we need to check if string contains only alphabet in c#, for example, when we accept some name, which should not contain any number of any other character other than alphabet.

    public static bool isValidName(string nameInput)
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(nameInput))
                isValid = false;
            else
            {
                
    //process 1
    isValid = Regex.IsMatch(nameInput, @"^[a-zA-Z]+$");
    
        //process 2
        foreach (char c in nameInput)
        {
            if (!Char.IsLetter(c))
                isValid = false;
        }
    
        }
        return isValid;
    }
    
  • Check if string has only number using C#, Suppose you want to accept someone’s age or phone number etc.

    public static bool IsOnlyDigits(string inputString)
    {
        bool isValid = true;
    
        foreach (char c in inputString)
        {
            if (!Char.IsDigit(c))
                isValid = false;
        }
        return isValid;
    }
    
  • Check if C# string contains only letter or digit, situation like allowing user to create unique username, which can contain only alphabet or number, no other character accepted, in such scenario following validation will work.

    public static bool IsOnlyLettersOrDigits(string s)
    {
    
           bool isValid = true;
    
           foreach (char c in s)
            {
                if (!Char.IsLetterOrDigit(c))
                    isValid = false;
            }
    
            return isValid;
    }
    
Comment
Name
Email
Website
Subscribe
 
free C# tutorial online


C# String Validation
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course