Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 04:02 PM   PM User | #16
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by hardhitter06 View Post
...
Code:
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.
jmrker is offline   Reply With Quote
Old 11-06-2012, 02:29 PM   PM User | #17
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
I appreciate the response but that doens't really help me...
hardhitter06 is offline   Reply With Quote
Old 11-06-2012, 03:19 PM   PM User | #18
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
function checkValidDate(yr,mmx,dd) {

The function requires the date to be passed to it in the format year,month,day, regardless of the format of the date entered by the user.

// USAGE EXAMPLE
checkValidDate(2011,2,31) // 31st February 2011


Show us your HTML for the date entry field(s).
__________________

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.

Last edited by Philip M; 11-06-2012 at 03:21 PM..
Philip M is offline   Reply With Quote
Old 11-06-2012, 07:04 PM   PM User | #19
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by hardhitter06 View Post
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?
jmrker is offline   Reply With Quote
Old 11-08-2012, 04:32 PM   PM User | #20
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
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>
Those are the 3 pieces...
hardhitter06 is offline   Reply With Quote
Old 11-08-2012, 04:34 PM   PM User | #21
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
I had this piece of code in earlier:

Code:
//var datechk = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/
//function dateCheck( fld )
//{
 //   var effectivedate = trim( fld );
 //   fld.value = effectivedate;
 //   return datechk.test(effectivedate);
//}
Which made sure the date was in the right format but didn't verify if the date was legit
hardhitter06 is offline   Reply With Quote
Old 11-08-2012, 08:12 PM   PM User | #22
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by hardhitter06 View Post
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?
jmrker is offline   Reply With Quote
Old 11-09-2012, 02:28 PM   PM User | #23
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
Can you show me the alert statement i would need?
hardhitter06 is offline   Reply With Quote
Old 11-09-2012, 03:06 PM   PM User | #24
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Try statements in red.
Code:
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
  } 
}
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
hardhitter06 (11-27-2012)
Old 11-09-2012, 03:07 PM   PM User | #25
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by hardhitter06 View Post
Can you show me the alert statement i would need?
function checkValidDate(yr,mmx,dd) {
alert (yr + " " + mmx + " " + dd);
__________________

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.
Philip M is offline   Reply With Quote
Old 11-26-2012, 02:37 PM   PM User | #26
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
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).

How would I fix these errors?
hardhitter06 is offline   Reply With Quote
Old 11-26-2012, 02:44 PM   PM User | #27
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
i tried both your versions of the alerts and both returned the same...
hardhitter06 is offline   Reply With Quote
Old 11-26-2012, 04:10 PM   PM User | #28
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
hardhitter06 (11-27-2012)
Old 11-26-2012, 04:23 PM   PM User | #29
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
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
hardhitter06 is offline   Reply With Quote
Old 11-26-2012, 04:33 PM   PM User | #30
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
There is no syntax error in the code I gave you.

Have you tried using your error console?
__________________

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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:03 PM.


Advertisement
Log in to turn off these ads.