Link to home
Start Free TrialLog in
Avatar of g46905
g46905

asked on

date validation in java script

I am passing a string variable...


DateValidation(testbox1){
}

I would like the function to convert this value into date format ( 08/18/05) and compare it with todays date.

If the value to not in the correct format, return false.
If the value is greater than todays date, retun false.

Can you please help me with this as I am new to javascript?



Avatar of rdivilbiss
rdivilbiss
Flag of United States of America image

Avatar of g46905
g46905

ASKER

As I mentioned, I am relatively new javascript.I went to the url you mentioned above and was unable to find code required for my requirement.I amy have missed it, Could you please specify it here?

Thanks,
Okay.  You basically need it all, but I think I can simplify it for you.

JavaScript only has low level date functions.  The date in the test field is text, not a date object.

So we have a couple of obstacles...is the text formatted correctly and can we make it into a data object for a comparison.

Since the date could be entered several ways, splitting it into month day and year is not trivial, ergo all the code in the example.  Most of it was for splitting the text into correct values.

If we dictate how the date can be entered, we reduce our cases considerably and can do this easier.

Assumming a required format of mm/dd/yyyy

Then we can validate the date when the person leaves the field (onblur).

<input type="text" name="date" onblue="validateDateText(this.value);">

Here, we specify the validateDateText function is called onblur with the value of the date field passed as the parameter.

Below are the functions.  validateDateText first calls validateTextDateFormat to make sure we have test in the format of mm/dd/yyyy.  If not, we return false and the second part of validateDateText never executes and it returns false to the original call made with onblur.

If the format is correct, then validateDateText creates a date object to hold today's date, then splits the text into pieces and makes a date object from those pieces.  The two date objects can then be compared to see if the date is greater than today, and if so, return false.

Here are the functions.

<script type="text/javascript">
<!--
function validateTextDateFormat(val) {
      var RegExPattern;
      RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
      if ((val.match(RegExPattern)) && (val!=''))
            return true;
      else
            return false;
}

function validateDateText(val) {
      if (validateTextDateFormat(val)) {
            var d = new Date();

            var valArray=val.split('/');
            var valM = valArray(0);
            var valDD = valArray(1);
            var valY = valArray(2);
      
            var createDateObjectFromTextDate = new Date(valY,valM,valDD);
            // is date greater than today
            if createDateObjectFromTextDate > d then
                  return false;
      }
      // date not correct format
      return false;            
}
//-->
</script>
ASKER CERTIFIED SOLUTION
Avatar of rdivilbiss
rdivilbiss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the "A"