Creating Your Own Validation

Creating Your Own Validation
There may be times when you can't find one of the built-in validation
controls that handles your specific data. For example, which control could
verify that a number the user has entered is an even number (if that was a
requirement of your page)? There isn't such a control, but ASP.NET provides
the CustomValidator control, which allows you to specify procedures to be
run in order to validate the data in the associated input control.

For this example, your goal is to ensure that users enter either CA, NV, or
AZ into the State text box on the page. You could use a DropDownList
control for this, or you could use a RegularExpressionValidator control
(setting the expression to be CA|NV|AZ). However, for the sake of this
example, suppose you want to take advantage of the CustomValidator control.

The CustomValidator control requires you to supply code for the control's
ServerValidate event梚t calls this code as it attempts to validate the data
on the server. No matter what browser has loaded the page, this code will
run before the entire page can be validated. If the browser supports
client-side script, it is nice to provide the script to run from within the
browser, on the client side, to provide the same sort of experience you get
when working with the other validation controls.

Therefore, you'll need to write the event procedure, in the page's
code-behind file, to provide server-side validation. In addition, in the
page itself, you'll need to insert the client-side script that will be sent
down to the browser. (In addition, you'll need to set the control's
ClientValidationFunction property to indicate the name of the client-side
function.) Both procedures receive two parameters: The first parameter
contains the object that raised the event (the CustomValidator control
itself), and the second parameter is a ServerValidateEventArgs object
containing information about the event. You can use the Value property of
the second parameter to retrieve the value the user entered, and you'll set
the IsValid property of the argument to indicate whether the value is valid.

To add validation for the State text box, follow these steps:

Add a CustomValidator control adjacent to the State text box.

Set the properties for the control as shown in Table 8.6.

Table 8.6. Set the CustomValidator Control's Properties to Match These
Items Property Value
ID cvalState
ClientValidationFunction ValidState
ControlToValidate txtState
Display Dynamic
ErrorMessage Enter CA, NV or AZ


Double-click the CustomValidator control and modify the ServerValidate
procedure so that it looks like this:

Private Sub cvalState_ServerValidate( _
ByVal source As System.Object, _
ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles cvalState.ServerValidate
  Select Case args.Value
    Case "CA", "NV", "AZ"
      args.IsValid = True
    Case Else
      args.IsValid = False
  End Select
End Sub

TIP

The ServerValidate event handler uses the Value property of its second
parameter to determine the value the user has entered and the IsValid
property to indicate whether the value is valid.



Close the code-behind file and then press Ctrl+PgDn (or use the View, HTML
Source menu item) to view the HTML source for the page.

Immediately below the <BODY> element, near the top of the page, add the
following script code, which provides the client-side validation:

<SCRIPT language="VBScript">
Sub ValidState(source, arguments)
   Select Case arguments.value
     Case "CA", "NV", "AZ"
       arguments.IsValid = True
     Case Else
       arguments.IsValid = False
   End Select
End Sub
</SCRIPT>

Now that you've added both the server-side and the client-side validation
procedures, browse to the page and verify that entering an invalid state
does trigger the validation code.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值