How to check special characters in the textbox in C#

 

In this article we will check the special character in textbox like ^,$,@ etc. to implement this we used char.IsLetter if it is true it means if u entered special character like % then it cause error and give message that use alphabets only similarly ISNumber tells us that the input is numaric or not IsLower tells us input is lower case or not IsUpper tells us that input is uppercase or not IsWhiteSpace tell us that input has not any space at the end of text .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication43
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "must enter the value");
            }
            else
            {
//stroe the every caharter u enter in the textbox
                char[] testarr = textBox1.Text.ToCharArray();
//to check the symbols in the given input through for loop
 
                for (int i = 0; i < testarr.Length; i++)
                {
//Isletter property of char is using to chek the special charcters
 
                    if (!char.IsLetter(testarr[i]))
                    {
                        errorProvider1.SetError(textBox1, "Symbols are not allowed");
                    }
                    else
                    {
                        errorProvider1.Clear(
;                                                                                       
                    }
                }
            }
 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "must enter the value");
            }
            else
            {
                char[] testarr = textBox1.Text.ToCharArray();
                for (int i = 0; i < testarr.Length; i++)
                {
                    if (!char.IsNumber(testarr[i]))
                    {
                        errorProvider1.SetError(textBox1, "Alphabets are not allowed");
                    }
                    else
                    {
                        errorProvider1.Clear();
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "must enter the value");
            }
            else
            {
                char[] testarr = textBox1.Text.ToCharArray();
                for (int i = 0; i < testarr.Length; i++)
                {
                    if (!char.IsLower(testarr[i]))
                    {
                        errorProvider1.SetError(textBox1, "Upper case alphabets are not allowed"
;
                    }
                    else
                    {
                        errorProvider1.Clear();
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "must enter the value");
            }
            else
            {
                char[] testarr = textBox1.Text.ToCharArray();
                for (int i = 0; i < testarr.Length; i++)
                {
                    if (!char.IsUpper(testarr[i]))
                    {
                        errorProvider1.SetError(textBox1, "Lower case Alphabets are not allowed"
;
                    }
                    else
                    {
                        errorProvider1.Clear();
                    }
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "must enter the value");
            }
            else
            {
                char[] testarr = textBox1.Text.ToCharArray();
                for (int i = 0; i < testarr.Length; i++)
                {
                    if (char.IsWhiteSpace(testarr[i]))
                    {
                        errorProvider1.SetError(textBox1, "space in the end of the text is not allowed");                   
                    }
                    else
                    {
                        errorProvider1.Clear();  
                    }
                }
            }
        }
    }
}

Ebook Download
View all
Learn
View all