5
Answers

how can validate special characters in textbox c#.net

hi ,

I am writing this code to validate special characters

Regex r = newRegex(@"[~`!@#$%^&*()-+=|\{}':;.,<>/?]");

      if (r.IsMatch(txtJobPackName.Text))

                {                

 MessageBox.Show("special characters are not allowed !!!");

                   TextBox1.Text ="";

                }

but my problem is that i am not able to validate some charater( []-_ " ). can you please give the sulotation. its urgent.

 


 

Answers (5)
0
Vulpes
NA 98.3k 1.5m 11y
One other thing I've just noticed is that your original expression is not validating the backslash character. It needs to be escaped by doubling it:

   Regex r = new Regex(@"[~`!@#$%^&*()+=|\\{}':;.,<>/?[\]""_-]");


Accepted
0
Vulpes
NA 98.3k 1.5m 10y
Do you mean that the textbox should contain at least one character but no character should be a special character?




0
Naga Harsha
NA 2 0 10y
hi i used your snippet but it gets null when i enter special charecters.. please validate me that textbox should not be empty.. its urgent.. thank you
0
Naeem Khan
NA 573 638.2k 11y
hello friend,
I m given you code for all validation like address, name , price etc..

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

namespace demo
{
    class Validation
    {
        public static void price_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;

            if (e.KeyChar == '.' || num == 8)
            {
                e.Handled = false;
            }
            else if (!char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        public static void numeric_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;

            if (num == 8)
            {
                e.Handled = false;
            }
            else if (!char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        public static void talepehone_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;

            if (e.KeyChar == ',' || e.KeyChar == ' ' || num == 8)
            {
                e.Handled = false;
            }
            else if (!char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        public static void name_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;
            if (e.KeyChar == '.' || e.KeyChar == ' ' || e.KeyChar == '&' || num == 8 || char.IsDigit(e.KeyChar) || e.KeyChar == '-')
            {
                e.Handled = false;
            }
            else if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        public static void address_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;
            if (e.KeyChar == ',' || e.KeyChar == '-' || e.KeyChar == '/' || e.KeyChar == ' ' || num == 8 || char.IsDigit(e.KeyChar) || num == 13)
            {
                e.Handled = false;
            }
            else if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        public static void vision_validation(KeyPressEventArgs e)
        {
            int num = e.KeyChar;
            if (e.KeyChar == ',' || e.KeyChar == '-' || e.KeyChar == '/' || e.KeyChar == ' ' ||e.KeyChar == '.' || num == 8 || char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}

just implement this code..
 hope it would be help full for you..if not then reply me will give you other solution 
0
Vulpes
NA 98.3k 1.5m 11y
Try:

   Regex r = new Regex(@"[~`!@#$%^&*()+=|\{}':;.,<>/?[\]""_-]");

Note that:

1. ']' needs to be escaped by prefixing it with '\' so that it's not treated as the end of the set.

2. It's generally best to place hyphen '-' at the end of the set so it's not treated as the range operator.

3. The double quote symbol '"' needs to be doubled as it's within a verbatim string.