Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2004, 07:17 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Comments in my javascripts 2

With the help of some good people from this community I've (or they) made 6 javascripts for me.
I'm in an emergency situation now I have to comment those codes.
I could have done it by myself but.....I can't.....
So if someone can help me please fell free!!!!
And here comes the 2nd one!


<html>
<head>
<title>Date Validation Test</title>

<link href="css.css" rel="stylesheet">
<style type="text/css">
<!--
.skicka_knapp {
background-color: #FFFF66;
border: #996633; border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 2px;
border-left-width: 2px}

p{
font-family: Verdana;
font-size:12px;
}

.form{
position:absolute;
left:150px;
top:200px;
}
.tit{
font-family:verdana;
font-size:10pt;
position:absolute;
left:150px;
top:170px;
}
</style>

<script language="javascript" type="text/javascript">

// this is the function to call from a button or event handler
function Validate()
{

    // construct today's date without the time element
    var dtTemp = new Date();
    var dtToday = new Date(dtTemp.getFullYear(), dtTemp.getMonth(), dtTemp.getDate());

    // check the date is OK
        // CHANGE frmTest.txtDate TO YOUR FORM AND FIELD NAMES
    if ( CheckValidDate(document.frmTest.txtDate.value, dtToday, null) )
    {
        // action if date OK
        alert("date OK");
    }
    else
    {
        // action if check failed
        alert("bad date");
    }
}

// function to check dates
// if MinDate or MaxDate not required just pass null there
function CheckValidDate(sValueToTest, dtMinDate, dtMaxDate)
{
    var cYEAR = 2, cMONTH = 1, cDAY = 0; // ### change these for other date formats
    var bOK = true;

    // attempt to convert to a date (assume dd/mm/yyyy format)
    var dtValueToTest;
    var aDate;
    try
    {
        // ### change this for other date formats ###
        aDate = sValueToTest.split("\/");
        dtValueToTest = new Date(aDate[cYEAR], aDate[cMONTH] - 1, aDate[cDAY]);
        if ( isNaN(dtValueToTest) )
        {
            bOK = false;
        }
    }
    catch (e)
    {
        bOK = false;
    }

    // if the date conversion succeeded, check its the actual date that was input
    // (e.g. it will accept 31/2/2003 but will change it to 3/3/2003)
    if (bOK)
    {
        // is it the same year?
        if (aDate[cYEAR] != dtValueToTest.getFullYear())
            bOK = false;

        // is it the same month?
        if (aDate[cMONTH] != 1 + dtValueToTest.getMonth())
            bOK = false;

        // is it the same day?
        if (aDate[cDAY] != dtValueToTest.getDate())
            bOK = false;
    }

    // min value test
    if (bOK && dtMinDate)
    {
        if (dtValueToTest < dtMinDate)
        {
            bOK = false;
        }
    }

    // max value test
    if (bOK && dtMaxDate)
    {
        if (dtValueToTest > dtMaxDate)
        {
            bOK = false;
        }
    }

    return (bOK);
}
</script>
</head>
<body bgcolor="#FF9966" >
<div class="back"><a href="links.html">Tillbaka</a></div>
<div class="tit">Please type today's date</div>
<div class="form">
<form action="date2.html" method="POST" id="frmTest" name="frmTest">
<input type="text" id="txtDate" name="txtDate" class="skicka_knapp">
<input type="button" value="Validate" onclick="Validate();" class="skicka_knapp">
</form>
<p>Use this format :<br/>dd/mm/yyyy</p>
</div>
</body>
</html>




A big thanxs to all of you who helped me a lot!!!!
Thanxs!!!!!!
 
Old November 3rd, 2004, 09:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You already have comments:

// this is the function to call from a button or event handler
function Validate()
{

    // construct today's date without the time element
    var dtTemp = new Date();
    var dtToday = new Date(dtTemp.getFullYear(), dtTemp.getMonth(), dtTemp.getDate());

    // check the date is OK
        // CHANGE frmTest.txtDate TO YOUR FORM AND FIELD NAMES
    if ( CheckValidDate(document.frmTest.txtDate.value, dtToday, null) )
    {
        // action if date OK
        alert("date OK");
    }
    else
    {
        // action if check failed
        alert("bad date");
    }
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
IIS ->Javascripts, any clue? justboomit General .NET 1 July 2nd, 2007 08:32 AM
Comments in my javascripts 6 xristina Javascript 5 November 4th, 2004 04:08 PM
Comments in my javascripts 4 xristina Javascript 1 November 4th, 2004 10:31 AM
Comments in my javascripts 5 xristina Javascript 1 November 4th, 2004 10:31 AM
Comments in my javascripts xristina Javascript 2 November 4th, 2004 10:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.