个性化阅读
专注于IT技术分析

asp.net web表单rangevalidator

点击下载

本文概述

该验证器评估输入控件的值以检查该值是否位于指定范围之间。

它使我们可以检查用户输入是否在指定的上下边界之间。此范围可以是数字,字母字符和日期。

注意:如果输入控件为空,则不会执行任何验证。

ControlToValidateproperty用于指定要验证的控件。 MinimumValue和MaximumValue属性用于设置控件的最小和最大边界。

RangeValidator属性

属性描述
AccessKey用于设置控件的键盘快捷键。
TabIndex控件的制表符顺序。
BackColor用于设置控件的背景色。
BorderColor用于设置控件的边框颜色。
BorderWidth用于设置控件边框的宽度。
Font用于设置控制文本的字体。
ForeColor用于设置控件文本的颜色。
Text它用于设置要为控件显示的文本。
ToolTip当鼠标悬停在控件上时, 它将显示文本。
Visible在窗体上设置控件的可见性。
Height用于设置控件的高度。
Width用于设置控件的宽度。
ControlToValidate它需要控制ID来验证。
ErrorMessage验证失败时, 用于显示错误消息。
Type用于设置控制值的数据类型。
MaximumValue用于设置范围的上限。
MinimumValue用于设置范围的下边界。

在以下示例中,我们使用RangeValidator验证指定范围内的用户输入。

// RangeValidator.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RangeValidator.aspx.cs" 
Inherits="asp.netexample.RangeValidator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
height: 82px;
        }
.auto-style2 {
width: 100%;
        }
.auto-style3 {
width: 89px;
        }
.auto-style4 {
margin-left: 80px;
        }
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style1">
<p class="auto-style4">
            Enter value between 100 and 200<br/>
</p>
<table class="auto-style2">
<tr>
<td class="auto-style3">
<asp:Label ID="Label2" runat="server" Text="Enter a value"></asp:Label>
</td>
<td>
<asp:TextBox ID="uesrInput"runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="uesrInput" 
ErrorMessage="Enter value in specified range" ForeColor="Red" MaximumValue="199" MinimumValue="101" 
SetFocusOnError="True"Type=" Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td>
<br/>
<asp:Button ID="Button2" runat="server" Text="Save"/>
</td>
</tr>
</table>
<br/>
<br/>
</div>
</form>
</body>
</html>

输出:

当输入不在范围内时,它将引发错误消息。

赞(0)
未经允许不得转载:srcmini » asp.net web表单rangevalidator

评论 抢沙发

评论前必须登录!