0

I have a field for date e.g. dd/mm/yyyy . I have validate code that won't allow a user to proceed once the date is not it the correct format dd/mm/yyyy

What I want to do is have a new field that has the default value dd/mm/yyyy in the field. I want the user to be able to leave this field as is dd/mm/yyyy and proceed or else if they enter anything else in the field it needs to be a valid date. So dd/mm/yyyy is accepted and any valid date e.g. 12/12/2012

else if(!validateDate(document.lending.date.value)){alert("Please enter a valid date. The date must be of the format dd/mm/yyyy");document.lending.date.focus();return false;}                                                          




function validateDate(thedate){
 if(thedate.length <11)
    {
try{
  day = thedate.substring(0,2);
  mmonth = thedate.substring(3,5);
  year = thedate.substring(6,10);
  if((thedate.charAt(2)!="/") || (thedate.charAt(5)!="/") || (day<1) || (day>31) ||     (mmonth<1) || (mmonth>12) || (year<1999) || (year>9999) ||     (!validateNumberWithoutDecimal(day)) || (!validateNumberWithoutDecimal(mmonth)) ||     (!validateNumberWithoutDecimal(year)))
     {
       return false;
     }
  else
     return true;
}catch(e){return false;}
 }
 else
 return false;

}

I'm thinking to write a new function validateMyNewDate and do it that way so that it accepts dd/mm/yyyy aswell as validating the date. just a bit confused with my logic now.

1
  • it depends on how smart your function needs to be (eg. detect days of february?). Since you do not have februare detection by now, your best bet is regexing the date like via regex: /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/ (taking either 01/12/2012 and or 01-12-2012 Commented Dec 7, 2012 at 12:00

1 Answer 1

4

You can try validating using the Date constructor instead:

var isValidDate = function(str) {
    return !!new Date(str).getTime();
}

It should work for the 'dd/mm/yyyy' format, f.ex:

isValidDate('12/12/1999'); // true
isValidDate('12/52/1999'); // false

If you want to make sure the user used the exact 'dd/mm/yyyy' format, you can combine it with a regex:

Update: added so it also validates a default input of "dd/mm/yyyy".

var isValidDate = function(str) {
    return str == 'dd/mm/yyyy' || 
           ( /^\d{2}\/\d{2}\/\d{4}$/.test(str) && new Date(str).getTime() );
}
Sign up to request clarification or add additional context in comments.

4 Comments

hi, thanks for the reply. I'm happy with my validateDate function as its in all the code but I want the user to be able to enter either 'dd/mm/yyyy' or a valid date. so if field is left as dd/mm/yyyy it will proceed without saying please put in a valid date. hope this makes sense. thanks
@topcat3 like the placeholder attribute?
yes, the field has the default value dd/mm/yyyy written exactly like that. I want user to be able to click next without even changing this field BUT if they do change it that it validates the date. thanks
Ok, then just add that in the validation method, see my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.