Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Date Validation

Date Validation

Scheduled Pinned Locked Moved JavaScript
javascripthelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AshBhande
    wrote on last edited by
    #1

    Hi, There are three fields: Start Date End Date RollOutDate The cases are as follows: Start Date > (Now - 2 years) Start Date < (Now + 2 years) Difference between Start Date & End Date is < 2 years Difference between RollOutDate & End Date is < 1 years These validations should be done on a save control button. Please can anyone help me in writing this piece of javascript code.

    L C P 3 Replies Last reply
    0
    • A AshBhande

      Hi, There are three fields: Start Date End Date RollOutDate The cases are as follows: Start Date > (Now - 2 years) Start Date < (Now + 2 years) Difference between Start Date & End Date is < 2 years Difference between RollOutDate & End Date is < 1 years These validations should be done on a save control button. Please can anyone help me in writing this piece of javascript code.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See http://www.w3schools.com/jsref/jsref_obj_date.asp[^].

      1 Reply Last reply
      0
      • A AshBhande

        Hi, There are three fields: Start Date End Date RollOutDate The cases are as follows: Start Date > (Now - 2 years) Start Date < (Now + 2 years) Difference between Start Date & End Date is < 2 years Difference between RollOutDate & End Date is < 1 years These validations should be done on a save control button. Please can anyone help me in writing this piece of javascript code.

        C Offline
        C Offline
        ClimerChinna
        wrote on last edited by
        #3

        <table>
        <tr>
        <td>
        Start Date:
        </td>
        <td>
        <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
        <td>
        End Date:
        </td>
        <td>
        <asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
        <td>
        Rollout Date:
        </td>
        <td>
        <asp:TextBox ID="txtRolloutDate" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
        <td colspan="2">
        <asp:Button ID="btnsave" runat="server" Text="Save" OnClientClick="fnSave();" />
        </td>
        </tr>
        </table>

        JAVASCRIPT: function fnSave() { debugger; var startdate=new Date(document.getElementById("txtStartDate").value); var enddate=new Date(document.getElementById("txtEndDate").value); var rolloutdate=new Date(document.getElementById("txtRolloutDate").value); var curdate = new Date(); if (!((startdate.getFullYear() > curdate.getFullYear() + 2) && (startdate.getFullYear() < curdate.getFullYear() - 2) && (((enddate - startdate) / (24 * 60 * 60 * 1000 * 365)) < 2) && (((enddate - rolloutdate) / (24 * 60 * 60 * 1000 * 365)) < 1))) { alert("please provide correct dates"); } }

        1 Reply Last reply
        0
        • A AshBhande

          Hi, There are three fields: Start Date End Date RollOutDate The cases are as follows: Start Date > (Now - 2 years) Start Date < (Now + 2 years) Difference between Start Date & End Date is < 2 years Difference between RollOutDate & End Date is < 1 years These validations should be done on a save control button. Please can anyone help me in writing this piece of javascript code.

          P Offline
          P Offline
          Praneet Nadkar
          wrote on last edited by
          #4

          Hi, Did you mean Start Date > (Now - 2 years) End Date < (Now + 2 years) instead of Start Date > (Now - 2 years) Start Date < (Now + 2 years) also, Is it a validation or to be calculated. I have not tested or ran the code, just gave it a try. var duration = 2; var startDate= new Date(); // You can take the input in here var endDate = new Date(); // you can take the input end date here too. var rollOutDate = new Date(); startDate.setFullYear( expiryDate.getFullYear() - duration ); endDate.setFullYear( expiryDate.getFullYear() + duration ); var difference = endDate - startDate; var difference1 = rollOutDate - endDate; //i dont know which is greater here var oneDay = 1000*60*60*24; var oneYear = oneDay / 365; var checkStartEndDate = Math.round(difference / oneYear); // in no of days var checkRollEndDate = Math.round(difference1 / oneYear); // in no of days if(checkStartEndDate < 2 && checkRollEndDate < 1 ) { return true; // You can also search and try the following //Page.IsValid = true; } else { return false; // You can also search and try the following //Page.IsValid = false; } Apologies if I missed something.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups