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 5: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 490
1 Maciej Los 305
2 Richard MacCutchan 270
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,834
1 Sergey Alexandrovich Kryukov 7,477
2 DamithSL 5,689
3 Maciej Los 5,329
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.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