views:

85

answers:

5

do we have isDate in Jquerry? that returns true if the input is a date else false?

A: 
Matt Roberts
var d = Date.parse("13/13/ 2010"); GIVES 1294857000000 :) SO IT CANT BE USED
Oh wow, that sucks! I thought it correctly told you if the date was valid.
Matt Roberts
A: 

Depending on how you're trying to implement this you can use the validate plugin with the date option set

http://docs.jquery.com/Plugins/Validation/validate

http://docs.jquery.com/Plugins/Validation/Methods/date

irishbuzz
+1  A: 

There's no built-in date functionality in jQuery core...and it doesn't really do anything directly to help with dates, so there aren't many libraries on top of it (unless they're date pickers, etc). There are several JavaScript date libraries available though, to make working with them just a bit easier.

I can't answer for sure what's best...it depends how they're entering it and what culture you're dealing with, keep in mind that different cultures are used to seeing their dates in different format, as a quick example, MM/DD/YYYY vs YYYY/MM/DD (or dozens of others).

Nick Craver
+1  A: 

The best way to get user date input is a date picker that only provides valid dates. It can be done with a string, but you are liable to rile your users by demanding they use your chosen format.

You need to specify in your validator if dates precede months.

This uses a second argument to enforce the order. With no second argument it uses the computer's default order.

// computer default date format order:
Date.ddmm= (function(){
    return Date.parse('2/6/2009')> Date.parse('6/2/2009');
})()

allow month names as well as digits: '21 Jan, 2000' or 'October 21,1975'

function validay(str, order){
    if(order== undefined) order= Date.ddmm? 0: 1;
    var day, month, D= Date.parse(str);
    if(D){
        str= str.split(/\W+/);

        // check for a month name first:
        if(/\D/.test(str[0])) day= str[1];
        else if (/\D/.test(str[1])) day= str[0];
        else{
            day= str[order];
            month= order? 0: 1;
            month= parseInt(str[month], 10) || 13;
        }
        try{
            D= new Date(D);
            if(D.getDate()== parseInt(day, 10)){
                if(!month || D.getMonth()== month-1) return D;
            }
        }
        catch(er){}
    }
    return false;
}
kennebec