ASP.NET MVC2 - 在服务器端验证之前修剪表单提交中的空白?
如果我添加验证属性:
public class ProductDownloadListModel
{
//xxxxx-xxxxx-xxxxx
[Required]
[StringLength(17)]
public string PSN { get; set; }
public DateTime PsnExpirationDate { get; set; }
public DataTable Downloads { get; set; }
}
并且用户输入 17 个字符的字符串但末尾包含空格,我会收到验证错误,因为该字符串大于 [StringLength(17)]属性。我怎样才能防止这种情况发生?我不希望在提交之前让 JavaScript 修剪字符串。
If I add a validation attribute:
public class ProductDownloadListModel
{
//xxxxx-xxxxx-xxxxx
[Required]
[StringLength(17)]
public string PSN { get; set; }
public DateTime PsnExpirationDate { get; set; }
public DataTable Downloads { get; set; }
}
and the user enters a 17-character string but includes white space on the end, I get a validation error because the string is greater than that specified by the [StringLength(17)]
attribute. How can I prevent this? I'd prefer not to have to have javaScript trim the string before submits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为 PSN 调整您的设备。
Do a trim on your set for the PSN.
为什么不在 html 输入上使用 maxlength 属性?
http://www.w3schools.com/tags/att_input_maxlength.asp
Why not use the maxlength attribute on the html input?
http://www.w3schools.com/tags/att_input_maxlength.asp