function checkValidDate(yr,mmx,dd)
{
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date(yr,mm,dd); // format YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm)
{
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!"); return false;
}
else
{
alert (dd + "/" + mmx + "/" + yr + " is an Valid Date!"); }
}
...
The alert statements are NOT the same. One says Valid and the other says Invalid.
The difference in displays is the check agains the passed month value and the calculated month valued from the Date() function.
I appreciate the response but that doens't really help me...
So what is it that you need help with? What is not working for you? What is your question?
BTW: In your code of post #15:
Code:
if ( ! checkValidDate( frm.elements["EffectiveDate."] ) )
oops += "You must enter your Effective Date of Move in the proper format (MM/DD/YYYY).\n";
do you really have an element named "EffectiveDate." (with the period)?
Could it perhaps be this as the problem?
Code:
if ( ! checkValidDate( frm.elements["EffectiveDate"].value ) )
oops += "You must enter your Effective Date of Move in the proper format (MM/DD/YYYY).\n";
Show the contents of the element field. Is it formatted as MM/DD/YYYY, without any spaces?
I will show you all the code i have for any date related stuff in my html.
I'm using "Form to File" which submits this data to a text file. By using the "." it says what fields have been left empty if the person's web browser has javascript disabled. It's like a backup.
Here goes:
Code:
function checkValidDate(yr,mmx,dd)
{
if (yr <1910 || yr >2012)
{ // you may want to change 2012 to some other year!
alert ("Year is out of range")
return false; }
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date(yr,mm,dd); // format YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm)
{ alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
}
else
{
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
}
}
Code:
function validateForm( frm )
{
var oops = "";
var apptype = getRBValue( frm.elements["MoveType."] );
if ( ! checkValidDate( frm.elements["EffectiveDate"] ) )
oops += "You must enter your Effective Date of Move in the proper format (MM/DD/YYYY).\n";
var inps = frm.elements;
var chkbx = false;
for ( var i = 0; i < inps.length; ++i )
{
var inp = inps[i];
if ( inp.name.indexOf("ChkBx") > 0 && inp.value != "" && inp.checked )
{
var chkbx = true;
break;
}
}
if ( oops == "" ) return true;
alert("ERROR(S):\n" + oops);
return false;
}
I deleted the other fields so you could just see the date one.
Code:
<td><input name="EffectiveDate." type="date" size="10">
<br>
<em>Format: MM/DD/YYYY i.e. 01/0<font face="Times New Roman, Times, serif">1</font>/2012 </em></td>
</tr>
I will show you all the code i have for any date related stuff in my html.
I'm using "Form to File" which submits this data to a text file. By using the "." it says what fields have been left empty if the person's web browser has javascript disabled. It's like a backup.
...
That last statement may be true for "Form to File", but I don't think it is legal JS.
Maybe some other forum members know differently (???)
Can you verify that the correct information is being sent to the validation function
by putting an alert at the start and displaying the contents passed to the function?
function checkValidDate(yr,mmx,dd) {
alert(yr+' '+mmx+' '+dd); // are you receiving what you expect?
if (yr <1910 || yr >2012) { // you may want to change 2012 to some other year!
alert ("Year is out of range")
return false;
}
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date(yr,mm,dd); // format YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) { alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
} else { alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
return true; // return results of function to if statement
}
}
Hi guys, sorry I took so long responding but I have tried the alerts with 12/01/2012 and this is what I get when I click my Submit button:
Message from webpage: [object] undefined undefined and i click the "OK" button
Message from webpage: undefined/undefined/[object] is an Invalid Date! and i click the "OK" button
And then one last Message from webpage which is my working javascript message box letting me know all of the other fields have errors (because i only filled out the date field).
You are passing the date to the script as a single string, but it expects three separate values for year, month and day.
Try this:-
Code:
<script type = "text/javascript">
function checkValidDate(which) {
var s = which.replace(/[\-\s]/gi,"/"); // change hyphens or spaces to /
var sp = s.split("/");
var yr = sp[2];
var mmx = sp[0];
var dd = sp[1];
alert(yr+' '+mmx+' '+dd); // are you receiving what you expect?
if (yr <1910 || yr >2012) { // you may want to change 2012 to some other year!
alert ("Year is out of range");
return false;
}
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date(yr,mm,dd); // format YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
return true; // return results of function to if statement
}
}
</script>
ENTER DATE AS MM/DD/YYYY <input name="EffectiveDate." type="text" size="10" onblur = "checkValidDate(this.value)">
Note that your input field is "text", not "date".
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Ive made the respective changes to both but i think there is a syntax error in the new code because the submit button is bypassing my javascript validations.
in other words, the error message is not popping up now for any of the fields now
Last edited by hardhitter06; 11-26-2012 at 04:28 PM..
Reason: in other words addition