简体   繁体   中英

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

        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)

^[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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