validation - Using Required Expression and Required Field Validators in ASP.NET -


i having trouble validating use inputs once calculate button clicked. appreciated. text fields must validated using validation controls , should include summary show errors. error messages appear in lblmessage.text nothing showing once calculate clicked. (side note: cannot clear button reset textboxes null. great too)

html

<body>     <form id="form1" runat="server">          <div>                 <img src="images/header.jpg" alt="get fit" title="getfit" width="100%"/>         </div>          <h1>body mass index calculator</h1>              <table align="center">                 <tr>                     <td class="auto-style4">enter first name:</td>                     <td class="auto-style6">                         <asp:textbox id="txtname" runat="server" tabindex="1"></asp:textbox>                     </td>                     <td class="auto-style5">&nbsp;</td>                     <td class="auto-style7">&nbsp;</td>                 </tr>                 <tr>                     <td class="auto-style4">enter age:                      </td>                     <td class="auto-style6">                      <asp:textbox id="txtage" runat="server" tabindex="2"></asp:textbox>                     </td>                     <td class="auto-style5">                         &nbsp;</td>                     <td class="auto-style7">                         <asp:comparevalidator id="cv" runat="server" controltovalidate="txtage" type="integer" operator="datatypecheck" errormessage="enter valid age" >*</asp:comparevalidator>                     </td>                 </tr>                 <tr>                     <td class="auto-style4">enter email:                      </td>                     <td class="auto-style6">                      <asp:textbox id="txtemail" runat="server" tabindex="3"></asp:textbox>                     </td>                     <td class="auto-style5">                         &nbsp;</td>                     <td class="auto-style7">                         <asp:regularexpressionvalidator id="regularexpressionvalidator3" runat="server" controltovalidate="txtemail" display="dynamic" errormessage="enter valid email" validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:regularexpressionvalidator>                     </td>                 </tr>                 <tr>                     <td class="auto-style4">enter height:&nbsp;                         </td>                     <td class="auto-style6">                         <asp:textbox id="txtheight" runat="server" tabindex="4"></asp:textbox>                         </td>                     <td class="auto-style5">                         <asp:label id="lblheight" runat="server" text="inches"></asp:label>                     </td>                     <td class="auto-style7">                         <asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" controltovalidate="txtheight" display="dynamic" errormessage="you must enter valid height">*</asp:requiredfieldvalidator>                     </td>                 </tr>                 <tr>                     <td class="auto-style4">enter weight:</td>                     <td class="auto-style6">                         <asp:textbox id="txtweight" runat="server" tabindex="5"></asp:textbox>                     </td>                     <td class="auto-style5">                         <asp:label id="lblweight" runat="server" text="pounds"></asp:label>                     </td>                     <td class="auto-style7">                         <asp:requiredfieldvalidator id="requiredfieldvalidator2" runat="server" controltovalidate="txtweight" display="dynamic" errormessage="you must enter valid weight">*</asp:requiredfieldvalidator>                     </td>                 </tr>                 <tr>                     <td colspan="4">                         <asp:label id="lblmessage" runat="server" text="label"></asp:label>                     </td>                 </tr>             </table>         <br />             <table align="center">                 <tr>                     <td class="auto-style2">                         <asp:button id="btncalculate" runat="server" text="calculate bmi" align="center" tabindex="6"/>                         &nbsp;                         <span style="color:darkblue;">|</span>&nbsp;                         <asp:button id="btncancel" runat="server" text="cancel" align="center" tabindex="7"/>                     </td>                 </tr>             </table>         <br />         <table align="center">                <tr>                 <td>                     <h1>your bmi:</h1>                 </td>             </tr>             <tr>                 <td>                     <asp:textbox id="txtbmi" runat="server" readonly="true" tabindex="8"></asp:textbox>                 </td>             </tr>             <tr>                 <td>                     <h2>this means are:                         </h2>                 </td>             </tr>             <tr>                 <td>                          <asp:textbox id="txtresults" runat="server" readonly="true" tabindex="9"></asp:textbox>                  </td>             </tr>         </table>     </form> </body> 

aspx.vb

partial class _default     inherits system.web.ui.page      public sub clearall()         txtname.text = ""         txtage.text = ""         txtemail.text = ""         txtheight.text = ""         txtweight.text = ""     end sub      protected sub btncancel_click(byval sender system.object, byval e system.eventargs) handles btncancel.click         clearall()         'txtname.text = string.empty         'txtage.text = string.empty         'txtemail.text = string.empty     end sub      private sub btncalculate_click(byval sender system.object, byval e system.eventargs) handles btncalculate.click         calculate()     end sub       public sub calculate()         'name         if txtname.text = string.empty             lblmessage.text = "please enter name."         else         end if          'age         if txtage.text = string.empty             lblmessage.text = "please enter age."         else         end if          'email         if txtemail.text = string.empty             lblmessage.text = "please enter email."         else         end if          'height         if txtheight.text = string.empty             lblmessage.text = "please enter height."         else         end if          'weight         if txtweight.text = string.empty             lblmessage.text = "please enter height."         else         end if          dim result double ' declare "result" double.         dim rounded_result double          result = txtweight.text / (txtheight.text * txtheight.text) * 703 ' bmi calculation.          rounded_result = math.round(result, 1) ' round result 1 decimal place.         txtbmi.text = rounded_result.tostring ' display bmi using lblbmi.          bmi_message(rounded_result) 'send rounded bmi result sub bmi_message.      end sub      public sub bmi_message(byval bmi_value double)         select case bmi_value             case 0.0 18.5                 txtresults.text = "underweight"             case 18.6 24.9                 txtresults.text = "normal weight"             case 25.0 29.9                 txtresults.text = "overweight"             case >= 30.0                 txtresults.text = "obese"             case else                 txtresults.text = "unable determin bmi"         end select     end sub  end class 

try ...

 private sub btncalculate_click(byval sender system.object, byval e system.eventargs) handles btncalculate.click         page.validate()      if page.isvalid         calculate()     end sub 

and in clear button tag should add causevalidation="false"

<asp:button id="btncancel" runat="server" causevalidation="false" text="cancel" align="center" tabindex="7"/> 

hope helps....


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -