11

I'm trying to use the new required attribute on form fields in my .NET 4.5 webforms application.

However, since there is only one <form> surrounding all the controls on the page (webforms style), the browser does not know which fields to be validated when a submit button is pressed.

A stripped version of a page with both login functionality and a page search is attached below.

When I click the Search button my browser tell me that i have to fill in the username and password fields first. And when I try to login it tells me I have to enter some text in the search field. Because all of the fields are children of the same <form> tag.

Any one else had this problem or came up with a solution? I would like to use the HTML5 attributes for form validation, no javascript solution.

<!DOCTYPE html>

<html>
<head>
    <title>Title</title>
</head>
<body>
  <form method="post" runat="server" id="mainform">


    <%// Search %>
    <asp:Panel runat="server" DefaultButton="searchButton" >

      <asp:TextBox runat="server" ID="searchQuery" ClientIDMode="Static" required="required" />
      <asp:Button runat="server" ID="searchButton" ClientIDMode="Static" Text="Search"/>

    </asp:Panel>


    <%// Login %>
    <asp:Panel runat="server" DefaultButton="signInButton">

      <label for="inputusername">Username</label>
      <asp:TextBox runat="server" ClientIDMode="Static" ID="inputusername" required="required" />

      <label for="inputpassword">Password</label>
      <asp:TextBox runat="server" TextMode="Password" ClientIDMode="Static" ID="inputpassword" required="required" />

      <asp:Button ID="signInButton" runat="server" />

    </asp:Panel>

  </form>
</body>
</html>

</html>
6
  • do you tried to put out the form the search ? Commented Mar 15, 2013 at 14:19
  • I'm sorry but I did not understand that... Commented Mar 15, 2013 at 14:26
  • Have you tried to search outside the form? Commented Mar 15, 2013 at 14:29
  • Can't do that. The form tag has to wrap all the server controls... Commented Mar 15, 2013 at 14:34
  • 5
    The HTML5 required attribute does not allow setup by any group, and ASP.NET does not (without tomfoolery) allow multiple form tags (with server controls). You could instead use RequiredFieldValidator and set the ValidationGroup properties of the associated controls. Commented Mar 15, 2013 at 15:28

3 Answers 3

5

Try this please.. It worked for me..

<asp:TextBox runat="server" 
             ClientIDMode="Static" 
             ID="inputusername"                    
             required />
Sign up to request clarification or add additional context in comments.

1 Comment

Is the duplicate ClientIDMode="Static" intentional?
-1

Add novalidate attribute to the Search button. It's that easy.

http://www.w3schools.com/tags/att_form_novalidate.asp

Comments

-4

Since the ASP.Net Web Forms programming model does not allow multiple HTML tags, this is something you cannot take advantage of.

1 Comment

See the second answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.