开发者

Need a Regular expression to validate comments in the webpage

开发者_开发技巧

Need a regular expression to check if webpage has special characters in the comments field. Comments should only have characters,numbers and @ = - ' " . i inside the comments. I am using C#.net to check it

THis is the code I have and it does not work

 if (!Regex.IsMatch(comments.Text,@"^[a-zA-Z''-'\s]$"))
 {
     lblError.Text = "Please Check your Comment.";
     return false;
 }


Try this:

[a-zA-Z0-9@=\-'"]+


You are checking if the comment contains only one character because of the interval between ^ and $. Just remove them and if I remember correctly what Regex.IsMatch does, it should work.

Regex.IsMatch(comments.Text,@"[^a-zA-Z''-'\s]")

On a side note, perhaps you should allow numbers, too.

Oh, and I should note that it will return true if any other character than those indicated is found.


The regex should be something like @"[^\w\s''-'@\"]"

\w gives you the alphabetic characters (including accented character), numeric characters and the underscore

\s gives you the whitespace

(I escaped the ", but it may or may not be needed. It's a little after 3am and I'm a little fuzzy, so you may need to play with that a bit...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜