使用验证控件

原创 2006年05月28日 22:43:00

People really like client-side validation. It looks great and creates a better overall user experience. The problem, however, is that it does not work with all browsers. Not all browsers support JavaScript, and different versions of browsers support different versions of JavaScript, so client-side validation is never guaranteed to work.

For this reason, in the past, many developers decided to add all their form validation logic exclusively to server-side code. Because server-side code functions correctly with any browser, this course of action was safer.

Fortunately, the Validation controls discussed in this chapter do not force you to make this difficult choice. The Validation controls automatically generate both client-side and server-side code. If a browser is capable of supporting JavaScript, client-side validation scripts are automatically sent to the browser. If a browser is incapable of supporting JavaScript, the validation routines are automatically implemented in server-side code.

You should be warned, however, that client-side validation works only with Microsoft Internet Explorer version 4.0 and higher. In particular, the client-side scripts discussed in this chapter do not work with any version of Netscape Navigator.

Configuring Client-side Validation

The Validation controls make use of a JavaScript script library that is automatically installed on your server when you install the .NET framework. This library is located in a file named WebUIValidation.js.

By default, WebUIValidation.js is installed in a directory named aspnet_client located beneath your Web server's wwwroot directory. If you change the location of your root directory, you need to copy the aspnet_client directory to the new directory; otherwise, the validation script will not work. If WebUIValidation.js can't be found, you receive the error Warning! Unable to find script library 'WebUIValidation.js' .The exact location of the WebUIValidation.js file is determined by your machine.config file (in the <webControls clientScriptsLocation> section).

Microsoft includes a command-line tool with the ASP.NET Framework named aspnet_regiis that you can use to automatically install and uninstall the script library. To install the script library execute aspnet_regiis -c, to uninstall the library execute aspnet_regiis -e. The aspnet_regiis tool is located in your /WINNT/Microsoft.NET/Framework/[version]/directory

Enabling and Disabling Client-side Validation

If you request a page that contains a validation control, and you are using Microsoft Internet Explorer version 4.0 or higher, JavaScript code is automatically sent to your browser.

If, for whatever reason, you want to disable client-side form validation, you can do so by adding the following directive at the top of your page:

 
<%@ Page ClientTarget="downlevel" %>

This directive disables client-side form validation. Unfortunately, however, it also prevents all the ASP.NET controls on the page from rendering any non-HTML 3.2 compatible content. For example, the directive also prevents the rendering of Cascading Style Sheet attributes to the page.

NOTE

The ClientTarget attribute accepts a string value that corresponds to one of the entries in the <clientTarget> section of the machine.config file. In the machine.config file, uplevel is defined as Internet Explorer 4.0 and downlevel is defined as the Unknown browser. The Unknown browser is assumed to not support Cascading Style Sheets.

Alternatively, you can disable client-side validation for individual validation controls by setting the EnableClientScript property to the value False. Because all validation controls share the EnableClientScript property, you can use this property to disable client-side scripts for particular validation controls or for all validation controls.

Finally, you can disable validation, both client and server validation, when certain buttons are pushed. You'll need to do this when creating a Cancel button. See the last section of this chapter, "Disabling Validation," for sample code that demonstrates how to do this.

asp.net 数据验证控件的使用实例

asp.net 数据控件的使用
  • hoho_12
  • hoho_12
  • 2016年04月27日 18:31
  • 1985

ASP.net验证控件详解和按钮点击事件后验证控件的执行顺序问题以及在确认密码框为空,CompareValidator认为是合法的问题的解决方案

完全手写,难免有错误,大家拣自己需要的看一下。博主我也不能误人子弟,大家多提些修改和参考意见啊。 ASP.NET提供了6个验证控件如下: 1.CompareValidator  将输入控件的值同常...
  • u010792238
  • u010792238
  • 2014年03月02日 14:00
  • 1885

ASP.net验证控件

ASP.net提供了6种验证控件,这些控件不但可以使程序员轻松地实现对用户输入的验证,而且还可以选择验证在服务器端进行还是在客户端进行。这6中验证控件分别是: 1.RequiredFieldVali...
  • chenyanmoting
  • chenyanmoting
  • 2015年01月07日 08:06
  • 1635

jQuery 表单验证控件 比较简单

基于jQuery的表单验证控件, 比较简单. 自己练手写的
  • lpy3654321
  • lpy3654321
  • 2014年08月23日 08:27
  • 4139

ASP.NET中Custom Validator验证控件的使用

新建一个网站,默认主页为
  • yayun0516
  • yayun0516
  • 2014年07月15日 10:10
  • 1322

RequiredFieldValidator控件验证不能为空时报错多种解决方法以及问题分析

RequiredFieldValidator控件验证不能为空时报错多种解决方法以及问题分析
  • u014180504
  • u014180504
  • 2015年08月12日 11:45
  • 995

验证控件的使用

ASP.NET验证控件详解     现在ASP.NET,你不但可以轻松的实现对用户输入的验证,而且,还可以选择验证在服务器端进行还是在客户端进行,再也不必考虑那么多了...
  • gengyudan
  • gengyudan
  • 2014年08月27日 17:14
  • 469

ASP.NET中验证控件的使用

前言ASP.NET中的常见验证控件有6种,这里列举其中4个的使用方式,分别是: 1. RequiredFieldValidation 2. RangeValidation 3. CompareV...
  • qq_33419925
  • qq_33419925
  • 2017年12月15日 23:20
  • 43

ASP.NET中验证控件的使用

前言:     前几日,无奈用JS判断控件的有效性,发现的确是一件费力、费神的事情!特别是针对邮件格式、邮政编码等的关于正则表达式的JS验证(其中涉及正则表达式的比较等,较烦~)。其实对于这些常...
  • judyge
  • judyge
  • 2015年11月04日 21:50
  • 327

验证控件的使用

在之前做的系统中,对于输入限制、查空、用户名是否已注册等问题,需要将一步步的验证过程写在代码中,很是复杂,而且还容易出错。现在学习了asp.net,才知道可以利用验证控件轻松搞定。下面就我们常见的网站...
  • CYL_happygirl
  • CYL_happygirl
  • 2014年09月24日 20:54
  • 1018
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用验证控件
举报原因:
原因补充:

(最多只允许输入30个字)