Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
[no name] 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 25-Feb-13 11:21am    
lol
Richard C Bishop 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.

You can use the IndexOf method as described above of course, but there is a method called Contains that is especially designed for this:

C#
if (myTxtBox.Text.Contains(";"))
{
    // box contains a ; so do something here
}
 
Share this answer
 
v2
Comments
Joezer BH 25-Feb-13 11:50am    
5+
ridoy 25-Feb-13 13:30pm    
+5
Hi,
You can also use string.Contains() method to check existance of a string in another string.

Example-:
C#
bool hasSemiColon=textBox1.Text.Contains(";");
 
Share this answer
 
v2
Comments
ridoy 25-Feb-13 13:30pm    
+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.
C#
string x = txtValue.Text;
bool hasSemicolon = x.IndexOf(';') == -1 ? false : true;
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900