Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:48am
jmpapa445
Comments
ThePhantomUpvoter at 25-Feb-13 10:51am
   
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 at 25-Feb-13 11:21am
   
lol
richcb at 25-Feb-13 10:52am
   
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Comments
Edo Tzumer at 25-Feb-13 11:50am
   
5+
ridoy at 25-Feb-13 13:30pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
You can also use string.Contains() method to check existance of a string in another string.
 
Example-:
bool hasSemiColon=textBox1.Text.Contains(";");
  Permalink  
v2
Comments
ridoy at 25-Feb-13 13:30pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2

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

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100