Shopping cart form with JQuery validation
I thought this would be a good starting point for anyone who is creating a shopping cart and needs a form to process the details. Although the form comes with some JQuery validation I would recommend that you also add server side validation for those 5% of users that have javascript turned off.
Of particular note is the 'expiryDate' function that has been added.
1$.validator.addMethod('expiryDate', function(value, element){ var presentDate = new Date(); var selectedDateHash = parseInt($('#expiryMonth').val(), 10) + parseInt(($('#expiryYear').val() * 12), 10); var presentDateHash = (presentDate.getMonth()+1) + (presentDate.getFullYear() * 12); if (selectedDateHash > presentDateHash) { $('#expiryMonth').removeClass('invalidFormField'); $('#expiryYear').removeClass('invalidFormField'); } else { $('#expiryMonth').addClass('invalidFormField'); $('#expiryYear').addClass('invalidFormField'); } return this.optional(element) || selectedDateHash > presentDateHash; }, 'this credit card has expired');
This makes sure that the expiry date has not passed. You can view the demo here
TweetBacks

There are no comments for this entry.
[Add Comment] [Subscribe to Comments]