简体   繁体   中英

Two “forms” in one <form> clash their required attributes

The issue : I have a single page with two email forms (one <form> , two divs inside this which look like forms to the user). When you submit either form, the C# behind looks at the values of the form and sends them in an email. This works well, and now I want a little validation, but I can't add required attributes to the inputs as the code doesn't know it's two separate "forms" and needs to handle the required attributes separately for each one.

I understand why this issue occurs, but I want to know if there's a way to tell the page to handle the required attributes in groups.

What I've tried : Both "forms" are handled by the code behind, and so need to be run server-side, so separate <form> elements wouldn't work. I have tried nesting the "forms" as <form> elements inside the server-side <form> , which separates the required attributes as desired, but seemingly breaks a number of things and I've read this is generally bad practice anyway.

What I'm not asking : To validate the form in the code behind.

Edit : David's answer works great to validate groups of textboxes, which is what I needed.

To additionally validate a checkbox, I used javascript. onclientclick we can check if the checkbox is checked AND validate using Page_ClientValidate('validationgroup') and then return true, else return false. onclick is only fired when onclientclick returns true.

use one form with two submit buttons. Use validationgroup to distinguish

http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx

如果您为每个提交按钮编写自己的验证功能,则应该可以

<input type='submit' onclick='return validateFirstButton()'/>

To validate checkboxes follow the instructions here:

http://codeclimber.net.nz/archive/2007/07/19/How-to-add-a-required-validator-to-a-CheckBoxList.aspx

Because this derives from BaseValidator you will be able to use groups. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM