Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果用户试图提交HTML,如何使验证失败?_C#_Html_Asp.net_Validation - Fatal编程技术网

C# 如果用户试图提交HTML,如何使验证失败?

C# 如果用户试图提交HTML,如何使验证失败?,c#,html,asp.net,validation,C#,Html,Asp.net,Validation,我有一个RequiredFieldValidator可以工作,但当我尝试提交包含以下字符的字符串时除外: &# function confSubmit() { document.getElementById("Employee").value = document.getElementById("Employee").value.replace(/</g, "&lt;"); document.getElementById("Employee").value

我有一个RequiredFieldValidator可以工作,但当我尝试提交包含以下字符的字符串时除外:

&#
  function confSubmit() {
     document.getElementById("Employee").value = document.getElementById("Employee").value.replace(/</g, "&lt;");
document.getElementById("Employee").value = document.getElementById("Employee").value.replace(/>/g, "&gt;");
    }
我们的web服务器拒绝该请求,并声明不允许我们提交HTML

如何执行客户端验证以防止包含这些字符的字符串或其他可以解释为HTML的字符?

我对标记有这样的问题。 我将事件处理程序添加到表单中。当我点击提交按钮时,javasript代码在值发送到服务器之前工作

<from ..... onsubmit = "return confSubmit();">
并在我的javascript中添加一个函数,该函数可更改这些字符:

&#
  function confSubmit() {
     document.getElementById("Employee").value = document.getElementById("Employee").value.replace(/</g, "&lt;");
document.getElementById("Employee").value = document.getElementById("Employee").value.replace(/>/g, "&gt;");
    }

一种方法是限制用户只输入某些字符或不允许输入某些字符。例如,您可以在文本控件中启用或禁用某些键,如

// check for only upper, lower char with [space]
function onlyAlpha(e, t) {
   if (navigator.appName == "Microsoft Internet Explorer") {
      var n = event.keyCode;
      if (n > 64 && n < 91 || n > 96 && n < 123 || n == 32) {
         return true
      } else {}
   } else {
      var n = t.charCode;
      if (n > 64 && n < 91 || n > 96 && n < 123 || n == 32 || n == 0) {
         return true
      } else {}
   }
   return false
}
只允许字母数字

// check for upper, lower, [space], [,], [.] and numerals
function onlyAlphaNumeric(e, t) {
   if (navigator.appName == "Microsoft Internet Explorer") {
      var n = event.keyCode;
      if (n > 64 && n < 91 || n > 96 && n < 123 || n == 32 || n == 46 || n == 39 || n >= 48 && n <= 57) {
         return true
      } else {}
   } else {
      var n = t.charCode;
      if (n > 64 && n < 91 || n > 96 && n < 123 || n == 0 || n == 32 || n == 46 || n == 39 || n >= 48 && n <= 57) {
         return true
      } else {}
   }
   return false
}
你可以把它们当作

<asp:TextBox ID="txtText1" runat="server" onkeypress="return onlyAlpha(this,event);" MaxLength="30" />
<asp:TextBox ID="txtText2" runat="server" onkeypress="return onlyAlphaNumeric(this,event);" MaxLength="30" />
使用这些函数,您可以允许/禁止用户填写某些字符。这里需要处理的一个问题是捕获ctrl+v。你可以找到很多这样的帖子来处理这个问题

如果你方便使用正则表达式,你可以从这些SO帖子和

为了防止表单提交,更好的方法是使用一些客户端JavaScript库/jQuery插件。他们有很多

为了安全起见,最好使用正则表达式或自定义检查(如

If txtText1.Text.Contains("#") OrElse txtText2.Contains("<") Then
    'take action
End If

替换字符怎么样?您不应该在客户端编码html,然后在服务器端解码web服务器为什么要限制用户?服务器是否拒绝特定字符集或所有非字母数字字符。在这种情况下,您只需在javascript中使用regex并针对[a-zA-Z0-9]测试输入,我认为您首先需要替换&;-对于原始文本已包含不希望在服务器端解码的代码的情况。是的,当然,我不带全部代码,只是为了展示我的解决方案: