Prevent special characters in a TextBox

前端 未结 6 1508
一生所求
一生所求 2020-12-19 22:55

I want to prevent users from entering url\'s (like a href=\"\") in a TextBox.

I want to use a regular expression validator but no idea what to write?

How ca

6条回答
  •  粉色の甜心
    2020-12-19 23:16

    Do you mean you literally want to prevent them from entering the text href=" in the TextBox, or you want to prevent URLs? Either way, a RegexValidator is one solution:

    Actually, as far as I know there is not a very easy way to use an OOTB-regex validator to do a negative contains (i.e. "fail if any match"). Someone smarter may be able to correct me on that. But you can definitely use a custom validator:

    
    
    

    Codebehind:

    protected void ValidateNoUrls(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = !Regex.IsMatch(e.Value, @"(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?");
    }
    

提交回复
热议问题