Restrict Special Characters in Windows Application using c#

Example: In this application it will remove all the special characters when we trying to copy paste the special characters.


Lets see how it is possible with Copy,paste.

Please follow these steps :

1. Create a New Windows Form with the name of 'Form1', and take one label with the name of 'lblName' and one textbox with the name of 'txtName' and one button with the name of 'btnValidate'. Please see below image.



2. Add a code in TextChanged event for that textbox. Here my textbox name is 'txtName' in that I have added a event like .


private void txtName_TextChanged(object sender, EventArgs e)
{
      txtName.Text = RemoveSpecialCharacters(txtName.Text);
}



3. In that event you observed method name of 'RemoveSpecialCharacters'.Basically it will remove all the special characters when you are copy paste in to the textbox.

      private string RemoveSpecialCharacters(String inString)
      {
            String tmp = inString;
            foreach (char c in inString.ToCharArray())
            {
                if (!IsCharDigit(c))
                {
                    tmp = tmp.Replace(c.ToString(), "");
                }
            }
            return tmp;
       }

4. I have created below method for accepting special characters which we want. For my requirement I have accepting whitespace,dot(.),hyphen(-),and alphabets and digits.


        public bool IsCharDigit(char c)
        {
            return (c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '.' || c == '-' || char.IsWhiteSpace(c));
        }

what it will do, it will accept on those characters only and remaining all are removing.

Here the Complete Code of that with example.Please see below.

 string connectionString = pass Your Connectionstring here;

  private void btnValidate_Click(object sender, EventArgs e)
   {

            try
            {
                int result = InsertData();
                if (result > 0)
                {
                    MessageBox.Show("Data Saved Successfully");
                }
            }
            catch (SqlException Sqlex)
            {
                if (Sqlex.Message.Contains("CHECK constraint"))
                {
                    MessageBox.Show("Special Characters are not Allowed");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
  }

        public int InsertData()
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO tblValidate (Name) VALUES   (@ColumnName)");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@ColumnName", txtName.Text);
                connection.Open();
                return cmd.ExecuteNonQuery();
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            txtName.Text = LeaveOnlyNumbers(txtName.Text);
        }

        private string LeaveOnlyNumbers(String inString)
        {
            String tmp = inString;
            foreach (char c in inString.ToCharArray())
            {
                if (!IsCharDigit(c))
                {
                    tmp = tmp.Replace(c.ToString(), "");
                }
            }
            return tmp;
        }

        public bool IsCharDigit(char c)
        {
            return (c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '.' || c == '-' ||    char.IsWhiteSpace(c));
        }

Output: See below Images.


Here I am copied these characters AnKit123()*&%%&** and pasted into textbox see the output.



For my requirement in this application see what is the output comes it will accept whitespace,dot(.),hyphen(-),and alphabets and digits.


Here I am copied these characters BlogSpot.com-anKit123&&;%$#@  and pasted into textbox see the output.



Hope It Will Help You Guys...!!!!

Comments

Popular posts from this blog

Send API POST Request in MS SQL Server

Create Custom Form Control for ng-select in Angular

Use immutable data structures to avoid unnecessary change detection In Angular

Map Operator in RxJs

HTTP and grPC Communication protocols used in Microservices