Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for special characters (/*-+_@&$#%) in a string?

Tags:

c#

regex

How do I check a string to make sure it contains numbers, letters, or space only?

like image 338
MrM Avatar asked Dec 21 '10 20:12

MrM


People also ask

How do you check special characters?

Click Start, point to Settings, click Control Panel, and then click Add/Remove Programs. Click the Windows Setup tab. Click System Tools (click the words, not the check box), and then click Details. Click to select the Character Map check box, click OK, and then click OK.

How do you check special characters in regex?

Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .


2 Answers

Simple:

function HasSpecialChars(string yourString) {     return yourString.Any( ch => ! Char.IsLetterOrDigit( ch ) ) } 
like image 117
prmph Avatar answered Oct 06 '22 00:10

prmph


The easiest way it to use a regular expression:

Regular Expression for alphanumeric and underscores

Using regular expressions in .net:

http://www.regular-expressions.info/dotnet.html

MSDN Regular Expression

Regex.IsMatch

var regexItem = new Regex("^[a-zA-Z0-9 ]*$");  if(regexItem.IsMatch(YOUR_STRING)){..} 
like image 28
kemiller2002 Avatar answered Oct 06 '22 00:10

kemiller2002