简体   繁体   English

文本框的C#Asp.net验证方法?

[英]c# Asp.net Validation Methods for textbox?

I'm new to using c# in asp.net 我是在ASP.NET中使用C#的新手

I was just wondering what the best methods of validation are for when it comes to checking a textbox that has to be number's (and 1 decimal point) only, I've read about ajax but I understand that this only works if the client supports it, so I'm now looking into new methods. 我只是想知道最好的验证方法是什么,它仅用于检查必须为数字(和小数点后一位)的文本框,我已经阅读过有关ajax的信息,但我知道这只有在客户端支持的情况下才有效,所以我现在正在研究新方法。

Also do these validation methods have the ability to prevent a event such as a button press from triggering and causing the web application to break? 这些验证方法是否还具有防止按钮按下之类的事件触发并导致Web应用程序中断的功能?

Always validate data at client side as well as server side (in code behind) . 始终在客户端和服务器端(在后面的代码中)验证数据。 For example you can use the asp.net provided validation control which will provide you with Client side validation, In server side validation (in C#) you can implement your own logic for data validation. 例如,您可以使用asp.net提供的验证控件,该控件将为您提供客户端验证。在服务器端验证(在C#中)中,您可以实现自己的数据验证逻辑。 For example in your case you can use double.TryParse to see if the string entered in the TextBox is a valid double number. 例如,在您的情况下,您可以使用double.TryParse来查看在TextBox输入的字符串是否为有效的双double.TryParse数字。

The reason to have Both types of validation is: 进行两种验证的原因是:

  • Client may have disabled javascript on the browser. 客户端可能已禁用浏览器上的javascript。
  • Client's browser doesn't support Javascript ( see this question ) 客户端的浏览器不支持Javascript( 请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM