adamshields
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"></SC RIPT>
some form w/a text box
<SCRIPT LANGUAGE="JavaScript">
var myformValidator = new Validator("myform");
myformValidator.addValidat ion("date" ,"req","Pl ease 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
<SCRIPT src="gen_validatorv3.js" language="JavaScript"></SC
some form w/a text box
<SCRIPT LANGUAGE="JavaScript">
var myformValidator = new Validator("myform");
myformValidator.addValidat
</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
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,dat e[0])
test2=(1*date[0]==d.getDat e() && 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.
So far I have the following:
<script>
function validDate(obj){
date=obj.value
test1=(/^\d{2}[\-/]\d{2}[\
date=date.replace(/[\-/]/g
date=date.split(',')
d=new Date(date[2],date[1]-1,dat
test2=(1*date[0]==d.getDat
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)
</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)
<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])
}
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)"
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)
<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)"
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks ;-)
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...."