unable to do validation for select in struts2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    unable to do validation for select in struts2

    hi,
    im new to struts2..in my application i need to use that Struts2 concept..im developing jsp page..i had designed..drop-down box in jsp..if im doing validation it is not getting..and it is not getting validated..can any one help me out..
    login.jsp
    Code:
    s:select name="Type" label="Type" headerKey="1"  headerValue="-- Please Select --"
                              list="#{'01':'News','02':'Jokes','03':'Astrology','04':'Weather',
                                      '05':'Traffic','06':'Sports','07':'Prayer'}" />
    login.js
    Code:
    if (form.Type.selectedIndex < 0)
        {
            alert("Select \"Type\" options.");
            form.Type.focus();
            return (false);
        }
    thanks in advance,
    madhu.
  • madhuriks
    New Member
    • Jun 2010
    • 149

    #2
    any help reg my post

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      regarding this any help

      Comment

      • Canabeez
        New Member
        • Jul 2009
        • 126

        #4
        Maybe
        [code="javascrip t"]
        if (form.Type.sele ctedIndex == 0){
        alert("Select \"Type\" options.");
        form.Type.focus ();
        return (false);
        }[/code]

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          hi Canabeez,
          Even though i tried what you suggested but it not getting..is there any other way for the validation.

          Comment

          • Canabeez
            New Member
            • Jul 2009
            • 126

            #6
            I'm not that good at jsp, can you please post some generated HTML or a link to a sample page.

            Comment

            • madhuriks
              New Member
              • Jun 2010
              • 149

              #7
              hi Canabeez,
              the following is rendered Html page..can u provide me why im not getting
              Code:
              <html>
              <head>
              <title>Upload Text Content</title>
              <script type="text/javascript" src="/Cms/js/UploadGameContent.js"></script>
              </head>
              <body>
              <form id="" action="" method="POST">
              <table class="wwFormTable">
              <table align="center" width=610 cellspacing=0 cellpadding=3 style="border:1px solid gray">
              <tr>
              <td class="tdLabel"><label for="UploadTextTitle" class="label">ContentTitle :</label></td>
              <td><input type="text" name="ContentTextTitle" value="" id="UploadTextTitle"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextDesc" class="label">ContentDescription :</label></td>
              <td><input type="text" name="ContentTextDescription" value="" id="UploadTextDesc"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextTags" class="label">ContentTags :</label></td>
              <td><input type="text" name="ContentTextTags" value="" id="UploadTextTags"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextExpiry" class="label">ExpiryDate :</label></td>
              <td><input type="text" name="TextExpiryDate" value="" id="UploadTextExpiry"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextCost" class="label">Text Cost :</label></td>
              <td><input type="text" name="TextCost" value="" id="UploadTextCost"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextFile" class="label">Text File :</label></td>
              <td><input type="text" name="TextFile" value="" id="UploadTextFile"/></td>
              </tr>
              <tr>
              <td class="tdLabel"><label for="UploadTextType" class="label">Text Type:</label></td>
              <td><select name="TextType" id="UploadTextType">
                  <option value="1"
                  >-- Please Select --</option>
                  <option value="01">News</option>
                  <option value="02">Jokes</option>
                  <option value="03">Astrology</option>
                  <option value="04">Weather</option>
                  <option value="05">Traffic</option>
                  <option value="06">Sports</option>
                  <option value="06">Prayer</option>
              </select></td>
              </tr>
              <tr>
              <td colspan="2"><div align="center"><button type="submit" id="_0" value="Upload" onClick="validate(this.form);">
              Upload
              </button>
              </div></td>
              </tr>
              </table>
              </table></form>
              </body>
              </html>

              Comment

              • Canabeez
                New Member
                • Jul 2009
                • 126

                #8
                It's stupid, but did you try
                [code="javascrip t"]
                if (form.TextType. selectedIndex == 0){
                alert("Select \"Type\" options.");
                form.TextType.f ocus();
                return (false);
                }
                [/code]

                Comment

                • madhuriks
                  New Member
                  • Jun 2010
                  • 149

                  #9
                  hi Canabeez,
                  i tried using wht u suggested..i tried like tht only..but i didnt getting validated.

                  Comment

                  • Canabeez
                    New Member
                    • Jul 2009
                    • 126

                    #10
                    Try this:[code="javascrip t"]
                    if( document.getEle mentById('Uploa dTextType').sel ectedIndex == 0){
                    alert("Select \"Type\" options.");
                    document.getEle mentById('Uploa dTextType').foc us();
                    return (false);
                    }[/code]

                    Comment

                    • madhuriks
                      New Member
                      • Jun 2010
                      • 149

                      #11
                      hi Canabeez,
                      i tried what u suggested.it is getting i take it in individual jsp.but if im implementing in the following jsp.it is not getting validated..here is my jsp code
                      Code:
                      <%@ page contentType="text/html; charset=UTF-8" %>
                      <%@ taglib prefix="s" uri="/struts-tags" %>
                      
                      <html>
                          <head>
                              <title>Upload Game Content</title>
                              <script type="text/javascript" src="/Cms/js/UploadGameContent.js"></script>
                          </head>
                          <body>
                          <s:form action="" method="POST">
                              <table align="center" width=610 cellspacing=0 cellpadding=3 style="border:1px solid gray">
                                  <s:textfield name="ContentGameTitle" id="UploadGameTitle" label="ContentTitle "/>
                                  <s:textfield name="ContentGameDescription" id="UploadGameDesc" label="ContentDescription "/>
                                  <s:textfield name="ContentGameTags" id="UploadGameTags" label="ContentTags "/>
                                  <s:textfield name="GameExpiryDate" id="UploadGameExpiry" label="ExpiryDate "/>
                                  <s:textfield name="GameCost" id="UploadGameCost" label="Game Cost "/>
                                  <s:textfield name="GameFile" id="UploadGameFile" label="Game File "/>
                                  <s:select name="GameType" id="UploadGameType" label="Game Type" headerKey="1" headerValue="-- Please Select --"
                                            list="#{'01':'SIS','02':'Jar','03':'SISX','04':'APK','05':'SIS60','06':'SIS9'}" />
                                  <s:submit type="button" value="Upload" align="center" onClick="validate(this.form);"/>
                              </table>
                          </s:form>
                      </body>
                      </html>
                      the js file is
                      Code:
                      function validate(form)
                      {
                          if (form.ContentGameTitle.value == 0)
                          {
                              alert("Enter Content Game Title");
                              form.ContentGameTitle.focus();
                              return (false);
                          }
                          if (form.ContentGameDescription.value == 0)
                          {
                              alert("Enter Content Game Description");
                              form.ContentGameDescription.focus();
                              return (false);
                          }
                      if (form.ContentGameTags.value == 0)
                          {
                              alert("Enter Content Game Tags");
                              form.ContentGameTags.focus();
                              return (false);
                          }if (form.GameExpiryDate.value == 0)
                          {
                              alert("Enter Content Game Expiry Date");
                              form.GameExpiryDate.focus();
                              return (false);
                          }if (form.GameCost.value == "")
                          {
                              alert("Enter Content Game Cost");
                              form.GameCost.focus();
                              return (false);
                          }if (form.GameFile.value == 0)
                          {
                              alert("Enter Content Game File");
                              form.GameFile.focus();
                              return (false);
                          }if( document.getElementById('UploadTextType').selectedIndex == 0){
                              alert("Select \"Type\" options.");
                              document.getElementById('UploadTextType').focus();
                              return (false);
                          }document.form.submit();
                          return true;
                      can u chk it nd provide me the solution

                      Comment

                      Working...