c# - ASP.NET MultiLine TextBox - 带换行符 (\r\n) 的 MaxLength 的正则表达式验证器 - 客户端和服务器端验证的差异

标签 c# asp.net regex validation

背景:我正在开发 ASP.NET 网页,并且有一个如下所示的多行文本框

<asp:TextBox ID="textBox1" runat="server" TextMode="MultiLine"></asp:TextBox>

和一个正则表达式验证器来检查最大长度,如下

<asp:RegularExpressionValidator ID="validator1" runat="server" ControlToValidate="textBox1" ValidationExpression="^[\s\S]{0,10}$" ErrorMessage="You can only enter a maximum of 10 characters"></asp:RegularExpressionValidator>

这个想法是将用户输入限制为最多 10 个字符。您可能知道,RegularExpressionValidator 执行客户端和服务器端验证。

问题:对于使用换行符的用户输入,客户端字符数似乎少于服务器端字符数。因此,它通过了客户端验证,但在服务器端验证失败。

这可能是因为在客户端新行字符是 \n 而在服务器端它是 \r\n

知道如何解决这个问题吗?请注意,我需要客户端和服务器端验证。

测试数据:

Line
Line1

更新: 除了上面的奇怪之处之外,.Net 处理 \r\n 的方式还有一些其他的奇怪之处

  1. 如果包含 \r\n 的文本通过 .Net Web 服务 传递,则 自动序列化为 \n 。所以数量/长度 包含 \r\n 的字符串在 Web 服务 之前和之后有所不同 打电话。

  2. 如果包含 \r\n 的相同文本存储在 SQL Server 中,则它会保留 \r\n是。

因此,根据 MultiLine TextBox 中的值的进一步处理方式,验证逻辑需要更改。就我而言,字符串在到达外部系统之前通过网络服务传递 - 因此@sln建议的正则表达式在这两个系统上都能很好地工作客户端和服务器端验证

但是,如果您将值直接存储在 SQL Server 中并希望使用 regex 验证字符串,则需要执行其他步骤,例如替换 >\r\n\n ,当需要从数据库读取和显示时反之亦然。

也许,验证最大长度字符的整个方法就是 XY 的情况。问题是,首先可以有一种更优雅的方式来做到这一点吗?

最佳答案

您可以使用它在两侧运行^(?:\S|[^\S\r\n]|\r?\n){0,10}$

 ^ 
 (?:
      \S              # Not whitespace
   |  [^\S\r\n]       # or, whitespace, not CR or LF
   |  \r? \n          # or, CR(optional)LF
 ){0,10}
 $

仅换行符 -

 **  Grp 0 -  ( pos 0 , len 10 ) 
Line
Line1

回车换行

 **  Grp 0 -  ( pos 0 , len 11 ) 
Line
Line1

更新:
CRLF 翻译助手

我发现这些例程是地球上最快的。
它们是单 channel 正则表达式,可以立即进行巨大的兆字节转换。
该伪代码采用 C++ 范例。

// To Normalize to CRLF 
// -------------------------
// regex  CRLFCRtoCRLF( "(?>\\r\\n?)|\\n" ); // Dot-Net style
regex  CRLFCRtoCRLF( "\\r\\n?+|\\n" ); // Pcre/Perl style

void ReplaceCRLFCRtoCRLF( string& strSrc, string& strDest )
{
    string repl = "\\r\\n";
    strDest = regex_replace( strSrc, CRLFCRtoCRLF, repl );
}


// To Normalize to LF 
// -------------------------
regex  CRLFCRtoLF( "\\r\\n|\\r(?!\\n)" );

void ReplaceCRLFCRtoLF( string& strSrc, string& strDest )
{
    string repl = "\\n";
    strDest = regex_replace( strSrc, CRLFCRtoLF, repl );
}


// To find current state (not really needed)
// (Returns true if standalone CR or LF)
// ------------------------------------------
regex  CRorLF( "\\A(?>[^\\r\\n]*)(?>(?:\\r\\n)+[^\\r\\n]*)*\\z" );

bool IsLoneCRorLF( string& strSrc )
{
    // In this case we are going to try to match an entire
    // string that is free of lone cr's or lf's.
    // Then return the negative of that.
    // This is much faster than using the lookround's,
    // and we need a little speed here.
    return !regex_search( strSrc, CRorLF );
}

关于c# - ASP.NET MultiLine TextBox - 带换行符 (\r\n) 的 MaxLength 的正则表达式验证器 - 客户端和服务器端验证的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490707/

相关文章:

c# - 使用unicode从字符串中删除特殊字符

asp.net - 使用 VB.NET 从 DataGrid 中获取选定行的数据

.net - 按下Enter键的同时选择按钮

c# - 使用 linq to xml 解析 xml 时出现问题

Java Regex 使用正则表达式保留预期字符串并删除其他字符串

c# - 如何在 Visual Studio 调试器中查看 C# 自动属性的支持字段?

c# - 查找时间序列中的开始和停止

c# - 如何通过代码将复选框添加到滚动查看器?

javascript - 使用 javascript 获取字符串拆分后的剩余部分

php - 如何用 <p> 包裹所有片段(不在 <div> 或 <h> 内部)?