How can i validate a textfield for Regex.....

Hi

Now its long time a go i worked with RegEx type of thing, can anyone tell med regex string for Validateting the TextField for signs like @, / ? .....It is Title texfield and we dont want any title to contain signs like / or @.......
AdnanAsked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
An unescaped - character within [] brackets means you want to accept a range of characters. ie a-z is any character from a to z, and '-' is any character from ' to ' which isn't what you want. It's ok if - is the first or last character, such as in:
regEx = new Regex("[Z0-9._/@£*&¤!?*¨^~#%+-]");


sachinpatil10d's latest suggestion focuses on what you want to allow, rather than what you want to disallow, which is a good strategy. Note how:
regEx.IsMatch(title)
has become:
!regEx.IsMatch(title)

I've adjusted the pattern to disallow ' and - characters. This only accepts 1 to 40 alphabetic characters/spaces:

regEx = new Regex(@"^[a-zA-Z ]{1,40}$");
            if (!regEx.IsMatch(title) || string.IsNullOrEmpty(title))
            {
                shouldUpdate = false;
                validateTextField = true;
                _new = false;
            }

Or use this one to accept numbers too:
regEx = new Regex(@"^[a-zA-Z\d ]{1,40}$");

Note I also changed \s to just a space so that tabs, carriage returns and line feeds are not accepted.
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
0
 
sachinpatil10dCommented:
Add

using System.Text.RegularExpressions;



Regex regex;
            regex = new Regex(@"^[a-zA-Z''-'\s]{1,40}$");

            Control ctrl = (Control)sender;
            if (!regex.IsMatch(ctrl.Text) || ctrl.Text == "")
            {
                MessageBox.Show("Enter valid Name");
                ctrl.Text = "";
                return;
            }

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
AdnanAuthor Commented:
hm oki, sachinpatil10d

i tried as you show, but still i can save and if i try to write titel with @somtething or /something it wont hit the if statement... ?
regEx = new Regex(@"^[a-zA-Z''-'\s]{1,40}$");
            if (regEx.IsMatch(title) || string.IsNullOrEmpty(title))
            {
                shouldUpdate = false;
                validateTextField = true;
                _new = false;
            }

Open in new window

0
 
AdnanAuthor Commented:
Here is the code i was trying to find out...... regex _/@£*&¤!?*¨^~#%+-] for title to not contain this signs...
regEx = new Regex("[Z0-9._/@£*&¤!?*¨^~#%+-]");

Open in new window

0
 
AdnanAuthor Commented:
regEx = new Regex("[._/@£*&¤!?*¨^~#%+-<>]");
0
 
sachinpatil10dCommented:
Try this
regEx = new Regex(@"^[a-zA-Z''-'\s]{1,40}$");
            if (!regEx.IsMatch(title) || string.IsNullOrEmpty(title))
            {
                shouldUpdate = false;
                validateTextField = true;
                _new = false;
            }

Open in new window

0
 
AdnanAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for gonadn's comment http:/Q_27399977.html#36979714

for the following reason:

fixed, found solution of how to check regex expressions in title field...
0
 
Terry WoodsIT GuruCommented:
As I pointed out in my last post, there is an error in the pattern of the comment that the author is proposing the accept as the solution. In the interests of anyone else who views this question, I'd like to see that error addressed, therefore I am objecting.

I put together some sample code to demonstrate the issue - I used PHP as C# isn't my tool of choice but the result is very likely to be the same as the regex engines accept nearly identical syntax. As you can see, all the numbers are matched and not just special characters as the author seemed to intend.

I also wonder if the author failed to adequately recognise the expert contributions to the answer - if the author felt like any of the experts' comments were helpful, he should be awarding them some points. If not, then he should at least add a corrected answer so that the error is fixed (by escaping the dash), like this:  regEx = new Regex("[._/@£*&¤!?*¨^~#%+\-<>]");

In the absence of further comments from the author, I recommend that the author's comment #36979714 be accepted as the answer, and comment #36983654 as assisting
<?php
#Demonstrate the effect of matching [+-<] without escaping the - character:

$allchars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}';   #All ASCII characters (excluding CR, tab and other control characters), in order
preg_match_all("#[+-<]#", $allchars, $matches);
print_r($matches);
?>

Output:
Array
(
    [0] => Array
        (
            [0] => +
            [1] => ,
            [2] => -
            [3] => .
            [4] => /
            [5] => 0
            [6] => 1
            [7] => 2
            [8] => 3
            [9] => 4
            [10] => 5
            [11] => 6
            [12] => 7
            [13] => 8
            [14] => 9
            [15] => :
            [16] => ;
            [17] => <
        )

)

Open in new window

0
 
Terry WoodsIT GuruCommented:
Sorry that was meant to be posted as an objection -
As I pointed out in my last post, there is an error in the pattern of the comment that the author is proposing the accept as the solution. In the interests of anyone else who views this question, I'd like to see that error addressed, therefore I am objecting.

I put together some sample code to demonstrate the issue - I used PHP as C# isn't my tool of choice but the result is very likely to be the same as the regex engines accept nearly identical syntax. As you can see, all the numbers are matched and not just special characters as the author seemed to intend.

I also wonder if the author failed to adequately recognise the expert contributions to the answer - if the author felt like any of the experts' comments were helpful, he should be awarding them some points. If not, then he should at least add a corrected answer so that the error is fixed (by escaping the dash), like this:  regEx = new Regex("[._/@£*&¤!?*¨^~#%+\-<>]");

In the absence of further comments from the author, I recommend that the author's comment #36979714 be accepted as the answer, and comment #36983654 as assisting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.