Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 45
  1. #16
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    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.

  2. #17
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    I appreciate the response but that doens't really help me...

  3. #18
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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).
    Last edited by Philip M; 11-06-2012 at 03:21 PM.

    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.

  4. #19
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    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?

  5. #20
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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...

  6. #21
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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

  7. #22
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    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?

  8. #23
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Can you show me the alert statement i would need?

  9. #24
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    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
      } 
    }

  10. Users who have thanked jmrker for this post:

    hardhitter06 (11-27-2012)

  11. #25
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  12. #26
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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?

  13. #27
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    i tried both your versions of the alerts and both returned the same...

  14. #28
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  15. Users who have thanked Philip M for this post:

    hardhitter06 (11-27-2012)

  16. #29
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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

  17. #30
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •