Prevent submission unless all fields are filled asp.net and c# (Multiple choice quiz) -
i trying create multiple choice quiz whereby users answer questions , presented score upon submission, using required field validators present message if not answer 1 of questions, system still calculate score if have not completed questions when hitting submit. need able stop submission until radio button lists completed i'm not sure how this. code here:
<asp:content id="content2" contentplaceholderid="maincontent" runat="server"> <asp:label id="qlabel1" runat="server" text="question 1"></asp:label> <asp:radiobuttonlist id="radiobuttonlist1" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q1requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist1" cssclass="text-danger" errormessage="ensure question 1 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel2" runat="server" text="question 2"></asp:label> <asp:radiobuttonlist id="radiobuttonlist2" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q2requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist2" cssclass="text-danger" errormessage="ensure question 2 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel3" runat="server" text="question 3"></asp:label> <asp:radiobuttonlist id="radiobuttonlist3" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q3requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist3" cssclass="text-danger" errormessage="ensure question 3 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel4" runat="server" text="question 4"></asp:label> <asp:radiobuttonlist id="radiobuttonlist4" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q4requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist4" cssclass="text-danger" errormessage="ensure question 4 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel5" runat="server" text="question 5"></asp:label> <asp:radiobuttonlist id="radiobuttonlist5" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q5requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist5" cssclass="text-danger" errormessage="ensure question 5 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel6" runat="server" text="question 6"></asp:label> <asp:radiobuttonlist id="radiobuttonlist6" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q6requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist6" cssclass="text-danger" errormessage="ensure question 6 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel7" runat="server" text="question 7"></asp:label> <asp:radiobuttonlist id="radiobuttonlist7" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q7requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist7" cssclass="text-danger" errormessage="ensure question 7 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel8" runat="server" text="question 8"></asp:label> <asp:radiobuttonlist id="radiobuttonlist8" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q8requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist8" cssclass="text-danger" errormessage="ensure question 8 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel9" runat="server" text="question 9"></asp:label> <asp:radiobuttonlist id="radiobuttonlist9" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q9requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist9" cssclass="text-danger" errormessage="ensure question 9 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel10" runat="server" text="question 10"></asp:label> <asp:radiobuttonlist id="radiobuttonlist10" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q10requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist10" cssclass="text-danger" errormessage="ensure question 10 completed"></asp:requiredfieldvalidator> </div> <asp:button id="button1" runat="server" text="submit final answers" onclick="submit_click" visible="true" /> <script runat="server"> protected void submit_click(object sender, eventargs e) { int score = 0; list<radiobuttonlist> list = new list<radiobuttonlist>() { radiobuttonlist1, radiobuttonlist2, radiobuttonlist3, radiobuttonlist4, radiobuttonlist5, radiobuttonlist6, radiobuttonlist7, radiobuttonlist8, radiobuttonlist9, radiobuttonlist10 }; foreach (var element in list) { if (element.selectedvalue == "correct") { score++; } } response.write("you scored: " + score); button1.visible = false; } </script> </asp:content>
before hitting submit:
after hitting submit when no answers filled:
my submit button dissapears supposed want able stop being submitted , score output until questions answered.
new these sorry if seems easy or stupid. thanks
Comments
Post a Comment