?
Solved

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

Posted on 2011-10-17
11
Medium Priority
?
310 Views
Last Modified: 2012-05-12
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 @.......
0
Comment
Question by:Adnan
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 36978784
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36978789
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
 

Author Comment

by:Adnan
ID: 36978808
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:Adnan
ID: 36979166
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
 

Assisted Solution

by:Adnan
Adnan earned 0 total points
ID: 36979714
regEx = new Regex("[._/@£*&¤!?*¨^~#%+-<>]");
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36981235
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
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 36983654
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
 

Author Comment

by:Adnan
ID: 37074072
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37074068
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37074073
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question