如何检查一个特定的asp.net验证控件是有效的? [英] how to check a particular asp.net validation control is valid?

查看:156
本文介绍了如何检查一个特定的asp.net验证控件是有效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络的形式有不同的asp.net验证控件。是否可以检查一个具体的验证控制是有效的?例如在离开文本框的重点,首先我会检查requiredFieldValidatorUserName是有效的?如果它是有效的话,我会用ajax这个用户名是不是已经预订检查服务器。

In a web form there are different asp.net validation controls. Is it possible to check a particular validation control is valid ? For example on leaving focus of textbox, first I will check requiredFieldValidatorUserName is valid ? If it is valid then I will check on server using ajax that this user name is not booked already.

编辑:

释解:我要检查在客户端验证控件的有效性(即输入是有效的)

Explaination: I want to check validity (that input was valid) of a validation control on client side.

请指导。

推荐答案

最好的方法是使用一个的 的CustomValidator 与客户端code,因为这将显示所有的错误信息,块表单提交,并确保验证在服务器端重复的 - 记住,仅仅因为你有可用的客户端验证,并不意味着用户看到它:始终验证您在服务器上输入和

The best way would be to use a CustomValidator with client side code, as this will display all the error messages, block form submission and also ensure that the validation is repeated at the server side - remember, just because you have client-side validation available, doesn't mean the user's seen it: Always validate your input at the server as well.

的CustomValidator 然后将是codeD调用方法阿贾克斯,并会正确显示错误信息到客户端:

Your CustomValidator would then be coded to call the Ajax methods, and would show the error messages correctly to the client:

<asp:Label ID="UserNameLabel" AssociatedControlID="UserName" runat="server">
  UserName *:</asp:Label>
<asp:TextBox ID="UserName" runat="server" />
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                            ControlToValidate="UserName" EnableClientScript="true"
                            ErrorMessage="You must supply a username!" />
<asp:CustomValidator ID="UserNameCustom" runat="server"
                     ControlToValidate="UserName" 
                     ClientValidationFunction="CheckExisting"
                     OnServerValidate="UserNameCustomValidate"
                     ErrorMessage="Username already taken" />

和您的ClientValidationFunction应该是这个样子:

And your ClientValidationFunction should look something like:

<script type="text/javascript">
  function CheckExisting(source, arguments) {
    // Pass the arguments.Value to your AJAX call:
    if (ajaxCallUserNameTaken(arguments.Value)) {
      arguments.IsValid = false;
    }
  }
</script>

(显然,你需要写ajaxCallUserNameTaken方法来调用你的页面的方法/ Web服务的/ etc。)

(Obviously, you'll need to write the ajaxCallUserNameTaken method to call your page method/web service/etc.)

这样做,这样将确保验证方法发生,因为预期;这将调用每当用户切换到文本框中的留下的值(它不会被调用如果文本框为空),并确保用户无法提交该页面,直到他们提供一个唯一的值。您还需要建立在 OnServerValidate 引用的方法,以确保该值的好,一旦它击中服务器过 - 这应该调用相同的code。该AJAX端点使用,以减少code等的重复。

Doing it this way will ensure that the validation methods happen as expected; this will get called whenever the user tabs out of the textbox leaving a value (it won't get called if the textbox is empty), and will ensure that the user can't submit the page until they supply a unique value. You'll also want to create the method referenced in OnServerValidate to ensure that the value's good once it hits the server too - this should call the same code that the AJAX endpoint uses to reduce duplication of code, etc.

我本来打算建议你可以使用在客户端的 Page_Validators 对象做一些检查,在的onblur 事件,但我真的不认为这是适合这里,因为它会导致更多的痛苦:

I was originally going to suggest that you could use the Page_Validators object on the client-side to do some checking in the onBlur event, but I don't really think this is suitable here as it results in more pain:


  1. 它假定,尽管有可能是在页面上多个验证,这里只有我们正在检查在控制的RequiredFieldValidator

  2. 的的RequiredFieldValidator是不是在的onblur 如果用户移出控制的没有设置值解雇 ​​- 所以即使只有当他们设置和清除值,的isValid 真正,您需要检查一个空字符串!

  1. It assumes that although there might be more than one validator on the page, there's only the RequiredFieldValidator on the control we're checking
  2. The RequiredFieldValidator isn't fired during OnBlur if a user moves out of a control without setting a value - only if they set and clear the value, so even if isvalid is true, you need to check for an empty string!

这篇关于如何检查一个特定的asp.net验证控件是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆