Link to home
Start Free TrialLog in
Avatar of adamshields
adamshieldsFlag for United States of America

asked on

specific date function needed

I need to validate a date field that is entered in a text box. So I figured it could be placed in an externel file then called like the following

<SCRIPT src="gen_validatorv3.js"  language="JavaScript"></SCRIPT>

some form w/a text box

<SCRIPT LANGUAGE="JavaScript">
var myformValidator = new Validator("myform");
myformValidator.addValidation("date","req","Please enter the date MM/DD/YYYY");
</SCRIPT>

Here are the preq:

Tell if the format is not MM/DD/YYYY
Indicate if the day is invalid
Indicate if the month is invalid
Not allow year, month or day beyond current date
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

You did not state a few things.
What is the field name in your string parameters?
When does the date check has to occure? At field onChange event? At form onSubmit event?
What should be the flag to allow future dates?

And also be aware that this NOT the Rent-A-Coder site ;-)
So your question should be more a question, and less "I want this and that and that...."
Avatar of adamshields

ASKER

Sorry for the poor formatting and question.

So far I have the following:

<script>
function validDate(obj){
 date=obj.value
 test1=(/^\d{2}[\-/]\d{2}[\-/]\d{4}$/.test(date))
 date=date.replace(/[\-/]/g,',')
 date=date.split(',')
 d=new Date(date[2],date[1]-1,date[0])
 test2=(1*date[0]==d.getDate() && 1*date[1]==(d.getMonth()+1) && 1*date[2]==d.getFullYear())
 if (test1 && test2) return true
 alert("Invalid date")
 obj.select();
 obj.focus()
 return false
}
</script>
</head>

Further down...

  <TD>
   <INPUT type='text' name='birthdate' size='10' onchange="(validDate(this))">    (MM/DD/YYYY)
    </TD>

This works fine to determine that DD/MM/YYYY, but I need to change the format to MM/DD/YYYY, and when I change it around then the script becomes screwy (tech term ;-). Also it should not be valid if it is greater then the current date.
How about this:

<script>
function validDate(theField){
 var dPart = theField.value.split("/");
 if(dPart.length==3){
   var aDate = new Date(theField.value)
   if(aDate && aDate.getDate()==dPart[1]) return true;
 }
 alert("Invalid date.\nEnter Date in this format:\n\tMM/DD/YYYY")
 theField.select();
 theField.focus()
 return false
}
</script>
</head>

   <INPUT type='text' name='birthdate' size='10' onchange="validDate(this)">    (MM/DD/YYYY)


And here the Future check:

<script>
function validDate(theField){
 var dPart = theField.value.split("/");
 if(dPart.length==3){
   var bDate = new Date(theField.value)
   if(bDate && bDate.getDate()==dPart[1]){
     var aDate = new Date();
     if(aDate<bDate){
       alert("Date is in the Future.")
       theField.select();
       theField.focus()
       return false
     }
     return true;
   }
 }
 alert("Invalid date.\nEnter Date in this format:\n\tMM/DD/YYYY")
 theField.select();
 theField.focus()
 return false
}
</script>
</head>

   <INPUT type='text' name='birthdate' size='10' onchange="validDate(this)">    (MM/DD/YYYY)


Thanks Zvonko. Is there a way for this function to work without the onchange or setting the form action to the function? It is pretty easy for the user to bypass this check and still submit the form.
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia 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 ;-)