Answered by:
Validating fextbox entry in a Formview InsertItem Template

Question
-
User224181609 posted
Is there some trick to making validation work inside a Formview Insert Item template?
I have a number of textboxes a user fills out on a Formview InsertItem template, and I want to make sure no textboxes are left blank using a Required Filed Validator. Also want to limit the characters that can be entered into the textbox using a Regular Expression Validator
The problem: When a user clicks in one of the texboxes, but then tabs along leaving it blank, the Required Field Validator seems not to work. I use the same exact setup elsewhere on a form that is not inside a Formview and it works just fine.
So I am thinking something different is required to get the validators to work inside the formview??
Heres and example of the code:
<InsertItemTemplate> <table> <tr> <td width="50"> First Name:</td> <td> <asp:TextBox ID="FNameTextBox" CausesValidation="True" runat="server" Text='<%# Bind("FName") %>' Width="400" /> </td> </tr> <tr class="BoldRed"> <td colspan="2"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FNameTextBox" CssClass="BoldRed" Display="Dynamic" ErrorMessage="Provide first name" ValidationGroup="edit1"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" CssClass="BoldRed" Display="Dynamic" ErrorMessage="Only letters, spaces, hyphens, no apostrophes" ValidationExpression="(\s*\w*[\-?]?)*" ControlToValidate="FNameTextBox"></asp:RegularExpressionValidator></td> </tr>
Saturday, September 3, 2016 11:15 PM
Answers
-
User224181609 posted
Thank you for the reply. I see from your response it is not so straight-forward to work with validation controls in the formview.
On giving it more thought, since all I am doing is submitting the textboxes data into a database table, I think it willl be easier (and I will better understand what I have done), to not use a Formview InsertItem Template at all. I'm going to just do the code-behind to insert data from textboxes on the web form into the database table directly. That way the validators will work out of the box.
Thank your for the explanation
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 5, 2016 12:51 AM
All replies
-
User475983607 posted
I believe what you're describing is the default behavior of the required field validator. The required field validator checks for a change in the default state of the "control to validate". This is a server side change event and/or client side form submit event. I don't think the validator fires on client side blur. It might have in the past - can't remember. Anyway, a simply test reveled the required field validator does not fire on blur in a 4.5 web forms project.
Keep in mind, the required field validator in the code snippet above is in a validation group which changes the validation behavior too.
https://msdn.microsoft.com/en-us/library/ms227424.aspx
You can add an on blur feature to your page as shown below. FNameTextBox is wire to the blur event. An $.each() loop iterates over the Page_Validators array in the validation API filtering for the controltovalidate property and firing the ValidatorValidate API method to invoke the validator.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationGroup.aspx.cs" Inherits="TestingWeb.ValidationGroup" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript"> $(function () { $('#<%=FNameTextBox.ClientID%>').blur(function () { var id = $(this).attr('id'); console.log(id); $.each(Page_Validators, function (index, value) { console.log(value); //filter if (value.controltovalidate == id) { //fire the validation ValidatorValidate(value) } }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="FNameTextBox" runat="server"></asp:TextBox> <asp:TextBox ID="LNameTextBox" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="edit1" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FNameTextBox" CssClass="BoldRed" Display="Dynamic" ErrorMessage="Provide first name" ValidationGroup="edit1"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" CssClass="BoldRed" Display="Dynamic" ErrorMessage="Only letters, spaces, hyphens, no apostrophes" ValidationExpression="(\s*\w*[\-?]?)*" ControlToValidate="FNameTextBox"></asp:RegularExpressionValidator> </div> </form> </body> </html>
Sunday, September 4, 2016 12:40 PM -
User224181609 posted
Thank you for the reply. I see from your response it is not so straight-forward to work with validation controls in the formview.
On giving it more thought, since all I am doing is submitting the textboxes data into a database table, I think it willl be easier (and I will better understand what I have done), to not use a Formview InsertItem Template at all. I'm going to just do the code-behind to insert data from textboxes on the web form into the database table directly. That way the validators will work out of the box.
Thank your for the explanation
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 5, 2016 12:51 AM -
User1724605321 posted
Hi ClarkNK,
It seems you have found a workaround to achieve your requirement , AFAIK,,RequiredField validators wont be triggered in a Tab Key press. You may have to consider using javascript to fire the validate.Also , link below is an article about Adding Validation Controls to the Editing and Inserting Interfaces :
https://msdn.microsoft.com/en-us/library/bb426882.aspx
Best Regards,
Nan Yu
Monday, September 5, 2016 6:00 AM