简体   繁体   English

如何阻止或

[英]How to block <b> or <i> in a comment box using a regex in C#

I been trying to block some word such as <b> or <i> for a project 我试图阻止某个项目的某些词,例如<b><i>

        Regex regExp = new Regex("^[a-zA-Z_]*$");   


        if (regExp.IsMatch(status.ToString()))
        {
            lblStatus.Text = "Comment added successfully.";
            lblStatus.ForeColor = System.Drawing.Color.Blue;
        }
        else
        {
            lblStatus.Text = "Comment failed to add!!!.";
            lblStatus.ForeColor = System.Drawing.Color.Red;
        }

this is the code that i write but somehow it is not able to block is my code wrong? 这是我编写的代码,但由于某种原因它无法阻止,这是我的代码错误吗? i just want to show an error when it was press submit( this is a comment box which have a button to submit. i just want to show me else statement not giving me the error page) 我只想在按提交时显示一个错误(这是一个具有提交按钮的注释框。我只想向我显示else语句,没有给我错误页面)

^[A-Za-z0-9 ]*$

That will match only numbers, letters and spaces. 那将只匹配数字,字母和空格。 You are going to have to determine based on your particular case if periods, commmas, semicolons and the like are acceptable. 您将必须根据自己的具体情况确定句号,逗号,分号等是否可以接受。 Keep in mind that extremely constrictive rules can be annoying for users, and it may be best to strip out any unwanted characters on the server-side. 请记住,极为严格的规则可能会使用户烦恼,并且最好在服务器端删除所有不需要的字符。


Update: 更新:

You can create a negation group like this [^] where any characters following the caret will not be allowed. 您可以创建一个像[^]这样的否定组,其中不允许在插入号后面的任何字符。 See codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial which is a great article on regexes. 请参阅codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial,它是有关正则表达式的出色文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM