Disallow special characters in TextBox

I want to prevent users from entering url (for example, href = "") in a TextBox.

I want to use the regular expression checker, but don’t know what to write?

How can i do this?

+3
source share
6 answers

Do you mean that you literally want to stop them from entering text href="into the TextBox or want to ban URLs? In any case, the RegexValidator is one solution:

, , OOTB-regex (.. " " ). - . :

<asp:TextBox runat="server" id="myTextBox" />
<asp:CustomValidator runat="server" OnServerValidate="ValidateNoUrls" ControlToValidate="myTextBox" ErrorMessage="URLs not allowed" />

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\-\.\?\,\'\/\\\+&amp;%\$#_]*)?");
}
+3

javascript + regex , , .

, , , .. -, javascript - , , . . . , , , -, .

+2

ASP.NET, Regex Validator, , , URL . MSDN.

: @"^(?!.*(mailto\:|(news|(ht|f)tp(s?))\://).*).*$"

+1

JPS , , . onblur

0

Perhaps you should consider doing a validation (or deletion) on the server anyway, as an attacker could execute the direct HTTP POST protocol and bypass your javascript

0
source

Also make sure that you do not check only on the client side via JS or similar. If we are talking about a web application, you need to do a server side check. That is, because you will never know how the request was formed and where it came from. Easily generate any GET / POST request without even looking at yours.

0
source

Source: https://habr.com/ru/post/1762472/


All Articles