How to validate asp textbox in side gridview

I am trying to validate asp textbox inside grid-view but my challenge is that i have 3 item templates in my aspx page,
ID
Question
Answer
The questions are generated dynamically after the grid view.bind() is called but i want to put some validation on the Answer field where users have to enter specific answer only based on the ID.  For example, If ID = 1 then they can only enter age number like 1 to 99. If ID = 2 then they can only enter some kind height values like 5.3, 5.8 etc. Here is how my current grid view look like
<asp:GridView ID="GV_Test" runat="server"  
                    AutoGenerateColumns="False"
                    ShowFooter="True" DataKeyNames="ID">
                    <Columns>

                        <asp:TemplateField  HeaderText="Question ID" Visible="true">
                            <ItemTemplate>
                                <asp:Label ID="lblQST_SK" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Questions">
                            <ItemTemplate>
                                <asp:Label ID="lblQuestions" runat="server" Text='<%# Eval("Question")%>'></asp:Label>
                            </ItemTemplate>                                
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Answer">                              
                                <ItemTemplate>                               
                                  <asp:TextBox ID="txtAn" runat="server" CssClass="form-control" Text='<%# Eval("Answer")%>' ></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>

                </asp:GridView>

Open in new window

I believe i have to use some kind of javascript that uses some kind event like onkeyup or onclick() something like this but not sure how to use it.   Please help.   thanks
moe57Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kaushal AroraTechnical AnalystCommented:
Dear moe57,

You can perform your task by RowDataBound Event of Gridview. Here are the steps to follow for your reference:

Step 1: Add the Validators in your design page (.aspx) with Answer TextBox
<asp:TemplateField HeaderText="Answer">                              
  <ItemTemplate>                               
    <asp:TextBox ID="txtAn" runat="server" CssClass="form-control" Text='<%# Eval("Answer")%>' ></asp:TextBox>
    <asp:RequiredFieldValidator ID="rfvAnswer" ControlToValidate="txtAn" runat="server" ErrorMessage="Kindly Enter Answer"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="revAnswer" ControlToValidate="txtAn" runat="server" ErrorMessage="Kindly Enter Proper Value"></asp:RegularExpressionValidator>
   </ItemTemplate>
</asp:TemplateField>

Open in new window


I have added the RequiredFieldValidator too. You can remove if not required. ☺

Step 2: Add the RowDataBound Event of the Gridview
protected void gvShipmentStatus_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblQST_SK = (Label)e.Row.FindControl("lblQST_SK");
                    RequiredFieldValidator rfvAnswer = (RequiredFieldValidator)e.Row.FindControl("rfvAnswer");
                    RegularExpressionValidator revAnswer = (RegularExpressionValidator)e.Row.FindControl("revAnswer");

                    if (lblQST_SK != null)
                    {
                        switch (lblQST_SK.Text)
                        {
                            case "1":
                                revAnswer.ValidationExpression = "[0-99]";
                                break;
                            case "2":
                                revAnswer.ValidationExpression = "[-+]?([0-9]*\\.[0-9]+|[0-9]+)";
                                break;

                            // and so on for your ID Values you can give the regular expression as you like. 
                        }
                    }
}
}

Open in new window


Hope this helps solve your problem.

Regards,
Kaushal Arora
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
moe57Author Commented:
Kaushal Arora, you are great man thank for your help.  How can i put a time validation? I need to put military time validation?  thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.