在 ASP.NET 中禁用 JavaScript 时如何处理验证
我在我的网站中使用了一些典型的 ASP.NET 验证控件。现在,我尝试在浏览器中禁用 JavaScript
来测试我的应用程序,当然 验证控件
不再起作用。我认为最好尝试使用此处建议的解决方案之一来使它们工作,而不是重新投入轮子并为页面或我的对象构建验证层-我想对了吗?-
您想到这些选项以及原因:
在单击按钮的事件中包含一个代码来检查页面是否有效,如果没有显式调用
Page.Validate();
方法检查是否启用了 JavaScript,如果没有启用,我应该调用 < code>Page.Validate();
如果你有更好的方法请告诉我。
I'm using some of the typical ASP.NET's Validation Controls in my website. Now I'm trying to disable the JavaScript
in my browser to test my application and of course the Validation Controls
no longer works. I think it's best to try to make them work using one of the suggested solutions down here instead of reinvesting the wheel and build a validation layer for the page or my objects -Am I thinking right?-
What do you think of these options and why:
Include in clicked button's event a code to check if the page is valid and if not explicitly call the
Page.Validate();
methodCheck if whether the JavaScript is enabled and if not I should call
Page.Validate();
If you there's a better way to do it please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Javascript 表单验证纯粹是为了方便用户。这可以阻止他们提交包含无效电话号码或其他内容的表格。
当收到任何请求时,所有输入实际上都应该在服务器上进行验证。这是理想的流程,您会发现没有启用 JavaScript 的浏览器没什么大不了的:
browser -> javascript 验证(可选)->服务器验证(如果失败,返回初始页面,有错误)
所以即使他们没有JS,页面仍然提交数据,那么你可以从服务器返回错误。这通常是较差的用户体验(整个页面重新加载,除非重新填充表单,否则可能会重新输入输入),这就是 JS 通常包含在验证方案中的原因。
Javascript form validation is purely for user convenience. This stops them from submitting a form with an invalid phone number or whatever.
All inputs should actually be validated on the server when whatever request is being made is received. Here's the ideal flow, and you'll see that a browser not having javascript enabled is no big deal:
browser -> javascript validation (optional) -> server validation (if this fails, go back to initial page with errors)
So even if they have no JS, the page still submits the data, then you can return an error from the server. This is a poorer user experience typically (full page reload, potential retyping of inputs unless you repopulate the forms) which is why JS is often included in validation schemes.
验证控件主要用于在服务器端进行验证。客户端验证是可选的(请参阅
EnableClientScript
属性)。因此,如果它们不能在禁用 Javascript 的情况下工作,那么您的页面中可能缺少一些样板代码,例如 Page.IsValid 上的 MSDN 文档:您还可以调用
Page.Validate
并检查 < code>Page.IsValid 在页面的OnLoad
事件中,这样您就可以在需要重新提交表单时阻止回发继续下一步。您甚至可能不需要显式调用Validate()
— 默认情况下,Button.CausesValidation
为 true。The validation controls are designed to validate primarily on the server side. The client-side validation is optional (see the
EnableClientScript
property). So if they aren't working with Javascript disabled, then you're probably missing a little boilerplate code in your page, such as this snippet from the MSDN documentation on Page.IsValid:You can also call
Page.Validate
and checkPage.IsValid
in your Page'sOnLoad
event, so that you can prevent a postback from proceeding to the next step when the form needs to be re-submitted. You probably don't even need to callValidate()
explicitly —Button.CausesValidation
is true by default.您将需要进行自定义服务器端验证... http://msdn.microsoft .com/en-us/library/aa479013.aspx(信息位于底部)
如下所示:
You will need to do custom Server Side validation... http://msdn.microsoft.com/en-us/library/aa479013.aspx (information toward the bottom)
Something like this: