tags:

views:

41

answers:

2

Hello, I have a created a registration page where i am taking few details from the cardholder about the card. When i take month and date from the user the selected date automatically gets diasplayed in the drop down box.This makes the same date displayed twice.Here's the code:

<strong>Expiry Date *<br />
<select name="edmm" id="month">
                  <% if( month != null) { %>
                  <option value="<%=month%>" selected="selected"><%=month%></option>
                  <% } else { %>
                  <option selected></option>
                  <% } %>                  

                  <% 
                        for(int i=1;i<=12;i++) { 
                                            String mmStr = "";
                                            if (i < 10)
                                                mmStr = "0" + i;
                                            else
                                                mmStr = "" + i; 
                                        %>
                  <option value="<%=mmStr%>" ><%=mmStr %></option>
                  <%        }
                  %>

                </select>

                <select name="edyy" id="year">
                  <% if( year != null) { %>
                  <option value="<%=year%>"  selected="selected"><%=year%></option>
                  <% } else { %>
                  <option selected></option>
                  <% } %>
                  <% 
                    for(int i=(yy);i<=(yy+50);i++) {
                  %>
                  <option value="<%=i%>" ><%= i %></option>
                  <% } %>
                </select><br />
                 <label for="label" class="rightmargin">&nbsp;&nbsp;<sup>month</sup></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 <label for="label" class="rightmargin"><sup>year</sup></label>
</strong><br />
+1  A: 

Okay here's the deal - ideally you should be looping a Map of months and years using JSTL c:forEach instead of the code you're using. Someone will probably point out a better approach but I'll answer the specific question as you've asked it.

Which is how to avoid duplicating the selected date in the drop down.

I'm assuming the user selects the date and submits back to the same page, and you want the submitted mm/yyyy pre-selected and not duplicated in your loop.

You need to put an if condition checking that the next option element (going to be displayed) is not equal to the one already selected.

So for the month loop, change

<option value="<%=mmStr%>" ><%=mmStr %></option>

to

<%
            if (!(month.equals(mmStr))){
          %>
          <option value="<%=mmStr%>" ><%=mmStr %></option>
                  <%        
          }
                  %>

and for the year, change

<option value="<%=i%>" ><%= i %></option> 

to

 <%
            if (Integer.parseInt(year) != i){

          %>

                  <option value="<%=i%>" ><%= i %></option>
                  <% } 

          %>

Remember with this approach, the selected value will be at the top. So if the user selects month "04" the drop down will actually appear out-of-sync as

<option value="04" selected="selected">04</option>



          <option value="01" >01</option>

          <option value="02" >02</option>

                  <option value="03" >03</option>

          <option value="05" >05</option>

and so on. But that's a separate story.

JoseK
Thanks for your answerbut the thing is that doing this the blank wont be visible before selecting.I want the box to be blank initially.How can i do this?
bhavna raghuvanshi
you want a blank <option> initially? Then put a <option value="" selected="selected"></option> as the first option.
JoseK
A: 

I'd like to help you out of this, I stared 5 minutes to this piece of code, but I cannot clearly figure the exact flow soon enough and my eyes started bleeding. Sorry, but I am going to strongly recommend to not use scriptlets but just taglibs and leave all that raw Java code out of the JSP. I think @JoseK has nailed it down, but I cannot confirm it. He however got a vote of me for the effort to figure the code anyway.

See also:


I guess that the functional requirement is just to preselect the options during display. If this is true, then it's as easy as following with JSTL c:forEach (just drop jstl-1.2.jar in /WEB-INF/lib to install it).

<select name="month">
    <c:forEach begin="1" end="12" var="month">
        <option value="${month}" ${month == card.month ? 'selected' : ''}>${month}</option>
    </c:forEach>
</select>
<select name="year">
    <c:forEach begin="2010" end="2020" var="year">
        <option value="${year}" ${year == card.year ? 'selected' : ''}>${year}</option>
    </c:forEach>
</select>

Here, ${card} should point to a javabean representing the creditcard which look like:

public class Card {
    private String number;
    private Integer month;
    private Integer year;
    // Add/generate getters and setters.
}

The ${month == card.month ? 'selected' : ''} basically means the following: "if the currently iterated month number matches the month number of the card, then print selected, else print nothing.". Simple as that :)

See also:

BalusC
Sorry if I sound harsh. It's nothing personal, you are nice, it are just the bad practices in the code :)
BalusC
@BalusC Thanks :) I'm actually familiar with this old style of scriptlet coding - hence could figure out a bit.
JoseK