2

if the month was less than today's month then I should show an error message through javascript in visualforce page.......I have tried but unable to...here is my code

/*javascript*/
<script type="text/javascript">
function validmonth()
{
if(exm < Month(today()))
{
condition=1;
alert("Invalid month");
jQuery(".exm").focus();
return false;
}
}
</script>

/* page */
<apex:selectlist value="{!Item.ExpirationMonth}" size="1" styleclass="exm" > 
<apex:selectOptions value="{!item.ExpireMonth}"/>
</apex:selectlist>

/* in constructor */
string dd=datetime.now().format('MM/dd/yyyy');
string mon=dd.substring(0,2);
string year=dd.substring(7,10);
item.ExpirationMonth=mon;

This function validmonth() Ill be calling in Command button.....

7
  • Is exm global? Seems like it needs to be a parameter to the validmonth() function to me.
    – Matt Lacey
    Oct 19, 2012 at 5:42
  • ya it is a parameter of validmonth()....In a hurry didn't write it var exm=jQuery(".exm").val();..is the condition if(exm < Month(today())) is correct
    – eagerinsf
    Oct 19, 2012 at 6:57
  • Would it not be a lot simpler to proxy this to the controller via an actionFunction and onComplete display a js alert if error? Oct 19, 2012 at 7:14
  • It depends on the size of the viewstate - a round trip could be quite expensive. Oct 19, 2012 at 7:16
  • So what would be the way for this...
    – eagerinsf
    Oct 19, 2012 at 7:18

1 Answer 1

1

You need to use the LIVE Jquery Event to bind to the Change event on the Select List. Here is an example I mocked up. I've used Javascript to get the month (0-11, hence increment by 1)

 <apex:page controller="TestValidateController">
    //INCLUDE JQuery Resources

    <script type="text/javascript">

    var d = new Date();
    var n = d.getMonth() + 1;
    jQuery.noConflict();

//Use the Live event to bind to changes on the Select List
    jQuery(".exm").live('change',

    function ()
    {
    alert('changed ' +  jQuery(".exm option:selected").text());
    if(jQuery(".exm option:selected").text() < n)
    alert('Error');

    }
    );
    </script>

    <apex:form>
    <apex:selectlist value="{!ExpirationMonth}" size="1" styleclass="exm" > 
    <apex:selectOptions value="{!ExpireMonth}"/>
    </apex:selectlist>
    </apex:form>
    </apex:page>

Controller :

public with sharing class TestValidateController {
public integer expirationMonth {get;set;}
public SelectOption[] expireMonth {get; set;}

public TestValidateController(){
expireMonth  = new List<SelectOption>{};
for (integer inti : (new List<integer>{1,2,3,4,5,6,7,8,9,10,11,12}))
expireMonth.add(new SelectOption(inti+'', inti+''));

}
}
1
  • Hey @eagerinsf just wondering if this solved your problem ? Oct 23, 2012 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.