Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Javascript validation - System Date
Old 10-23-2010, 10:27 AM Javascript validation - System Date
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Guyz,

I have 2 textboxes (PHP script) i need to validate them using Javascript. one textbox is to type date.. but the user should not be able to type a future date and press submit.

i wrote a javascript validation :

Code:
<script language="JavaScript">
        <!--
        function CompareDates(str1,str2) 
        { 
            var dt1  = parseInt(str1.substring(0,2),10); 
            var mon1 = parseInt(str1.substring(3,5),10); 
            var yr1  = parseInt(str1.substring(6,10),10); 
            var dt2  = parseInt(str2.substring(0,2),10); 
            var mon2 = parseInt(str2.substring(3,5),10); 
            var yr2  = parseInt(str2.substring(6,10),10); 
            var date1 = new Date(yr1, mon1, dt1); 
            var date2 = new Date(yr2, mon2, dt2); 
        
            if(date2 < date1) 
            { 
                return false; 
            } 
            else 
            { 
                return true; 
            } 
        } 
        function validate()
        {
            var str2 = document.getElementById("date").value; 
            
            var str1 = "23/10/2010"; 
            
            
            if (document.frm.date.value=="")
            {
                alert("Please enter date");
                document.frm.date.focus();
                return false;
            }
            else if (document.frm.number.value=="")
            {
                alert("Please enter number");
                document.frm.number.focus();
                return false;
            }            
            else if (eval(document.frm.number.value)<=0)
            {
                alert("Please enter number positive");
                document.frm.number.focus();
                return false;
            }
            else if (CompareDates(str1,str2))
            {
                alert("To date cannot be greater than from date"); 
                document.frm.date.focus();        
                return false; 
            }
            else
                return true;            
        }    
        //-->
        </script>
This works perfectly but the problem is validation takes current date from :


Code:
 var str1 = "23/10/2010";
i need that to be the system date so i dont have to change the str value everytime.. any help?

Thanks in advance

Sam
sam20e is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-23-2010, 02:17 PM Re: Javascript validation - System Date
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
alert("To date cannot be greater than from date");
Surely a "To" data MUST be greater than a "from" date??
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-23-2010, 10:47 PM Re: Javascript validation - System Date
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Hi,

its like this. there is only 1 text box to enter a date.. so the person should not be able to enter a future date (should be less than system date always)
sam20e is offline
Reply With Quote
View Public Profile
 
Old 10-25-2010, 01:01 PM Re: Javascript validation - System Date
Novice Talker

Posts: 5
Name: Sam
Trades: 0
problem solved

by replacing :

Code:
var str1 = "23/10/2010";
with

Code:
var date = new Date();
var str1 = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
thanks anyways
sam20e is offline
Reply With Quote
View Public Profile
 
Old 10-26-2010, 04:22 AM Re: Javascript validation - System Date
Novice Talker

Posts: 7
Trades: 0
Such a nice solution of javascript validation.
__________________

Please login or register to view this content. Registration is FREE
||
Please login or register to view this content. Registration is FREE
corl is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript validation - System Date
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24389 seconds with 12 queries