Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
Hi! How can I check if the data entered in textbox has a semicolon. I am aware that I should use the regex condition, but I don't get how to properly implement the semicolon on the condition. Any help? Thank you in advance.
Posted 25 Feb '13 - 4:48
jmpapa444

Comments
ThePhantomUpvoter - 25 Feb '13 - 10:51
What have you tried? Using RegEx just to check for a semicolon is like trying to use a nuclear missile to drive a single nail in to a board.
Shelby Robertson - 25 Feb '13 - 11:21
lol
richcb - 25 Feb '13 - 10:52
You can use the RegularExpression validator provided by ASP.Net and add the regular expression to the validator and map it to the control you want to validate.

3 solutions

You can use the IndexOf method as described above of course, but there is a method called Contains that is especially designed for this:
 
        if (myTxtBox.Text.Contains(";"))
        {
            // box contains a ; so do something here
        }
  Permalink  
Comments
Edo Tzumer - 25 Feb '13 - 11:50
5+
ridoy - 25 Feb '13 - 13:30
+5
use string method IndexOf to find the semicolon from text.
 
IndexOf:
This method finds the first index of the char argument. It returns -1 if the char was not found.
 
e.g.
string x = txtValue.Text;
bool hasSemicolon = x.IndexOf(';') == -1 ? false : true;
  Permalink  
Hi,
You can also use string.Contains() method to check existance of a string in another string.
 
Example-:
bool hasSemiColon=textBox1.Text.Contains(";");
  Permalink  
Comments
ridoy - 25 Feb '13 - 13:30
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Mahesh Bailwal 353
1 Sergey Alexandrovich Kryukov 339
2 Maciej Los 290
3 CPallini 245
4 Rohan Leuva 175
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,194
2 CPallini 3,923
3 Rohan Leuva 3,176
4 Maciej Los 2,633


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid