首页 > c# > 文本框失去焦点时验证数据

文本框失去焦点时验证数据 (Validate data when textbox loses focus)

2012-09-15 c#asp.net

问题

在我的ASP.Net Web应用程序中,有一个文本框可以接收ProductId。当用户将数据输入到ProductId文本框并移动到下一个文本框时,我想针对表验证数据,如果找到匹配的记录,我想在不同的控件(只读和普通文本框)中显示产品详细信息,否则我想要显示有关“新产品”的消息并继续。我不想在用户输入所有数据并按“保存”按钮后进行验证,但应立即完成。我希望Web Form文本框有一个LostFocus事件,但不知道如何处理这个要求。

解决方法

ASP.NET验证器没有ajax样式立即检查。

  1. 将自定义验证程序附加到连接到验证方法服务器端的文本框。
  2. 使用javascript,您需要将一个事件处理程序连接到focusout事件并进行ajax调用。该调用会将值发送回服务器并获得有效或无效的响应。

这个网站:http//brian.dobberteen.com/code/jquery_ajax_custom_validator/将帮助你解决很多问题,但有一点是,只有在提交页面时才会触发。您需要将另一个事件处理程序连接到文本框,以便在focusout上调用验证方法

问题

In my ASP.Net web application, there is a textbox to receive ProductId. When the user enters data into the ProductId textbox and moves to the next textbox, I want to validate the data against a table and if matching record is found, I want to display Product details in different controls (readonly and normal textboxes) else I want to display a message about "New Product" and continue. I do not want to do the verification AFTER the user enters all data and press the Save button, but it should be done immediately. I was hoping that the Web Form textbox would have a LostFocus event, but do not know how to handle the requirement.

解决方法

ASP.NET Validators don't have an ajax style immediate check.

  1. Attach a custom validator to your textbox that hooks up to a validation method server side.
  2. Using javascript, you'll need to hook up an eventhandler to the focusout event and make an ajax call. That call will send the value back to the server and get a response of valid or not.

This site : http://brian.dobberteen.com/code/jquery_ajax_custom_validator/ will help you with a lot of it, but one thing is this will fire only when the page is submitted. You'll want to hook up an additional event handler to your textbox to call the validation method on focusout

相似信息