Find your content:

Search form

You are here

Month validation in javascript


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 is my code

<script type="text/javascript">
function validmonth()
if(exm < Month(today()))
alert("Invalid month");
return false;

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

/* in constructor */
string mon=dd.substring(0,2);
string year=dd.substring(7,10);

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

Attribution to: eagerinsf

Possible Suggestion/Solution #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;

//Use the Live event to bind to changes on the Select List

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


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

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+''));


Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content