| | |
problem with date validation
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2008
Posts: 50
Reputation:
Solved Threads: 0
Hello everyone, I am facing a problem with the start date and the end date.I have to do a validation such that the end date entered with the date picker should be equal to or greater than the start date.I have written the following code but it is not helping me out.Here is the code of javascript
The html code is as follows. start date
<script language="javascript">
function validate()
{
if(document.tstest.timestamp1.value < document.tstest.timestamp.value)
{
alert('Please check the End date');
return false;
}
if(document.tstest.timestamp.value.length==0 && document.tstest.timestamp1.value.length==0)
{
alert("Please enter the date");
return false;
}
return true;
}
</script>]<input readonly type="Text" id="start" name="timestamp" size = "20" value=""> End date <input readonly type="Text" id="end" name="timestamp1" size = "20" value=""> The problem is that the validation is done only for days but not for the month and year.If you enter the start date as dd/mm/yyyy 12/8/2008 and End date as 11/8/2008 it will give an alert box but if you enter start date as 12/8/2008 and end date as 09/8/2008 than the form is submitted it is not validating. Please anyone can find a solution for this. thanks in advance. •
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
You're trying to compare strings as if they were numbers ... that will give you unreliable results at best.
To compare as numbers you should do something like this ...
I haven't actually tested the above script, so there might be some hidden (or not so hidden) hitch in it, but that type of thing is what you're looking for -- I believe.
Good luck
To compare as numbers you should do something like this ...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> // grab and split date values var start = document.getElementById('start').value; var sarr = start.split('/'); var sday = parseInt( sarr[0] ); var smo = parseInt( sarr[1] ); var syr = parseInt( sarr[2] ); var end = document.getElementById('end').value; var earr = end.split('/'); var eday = parseInt( earr[0] ); var emo = parseInt( earr[1] ); var eyr = parseInt( earr[2] ); // here we must compare date values validate(); function validate () { switch ( true ) { case syr > eyr: alert( 'please pick an end year later than start year' ); return false; case smo > emo: alert( 'please pick an end month later than start month' ); return false; case sday > eday: alert( 'please pick an end day later than start day' ); return false; default: document.getElementById('my_form_id').submit(); } } </script>
I haven't actually tested the above script, so there might be some hidden (or not so hidden) hitch in it, but that type of thing is what you're looking for -- I believe.
Good luck
![]() |
Similar Threads
- Beginner and I would love help :) date validation (C++)
- Problem with arrays and list (C#)
- Date Validation in HTML (JavaScript / DHTML / AJAX)
- Validation difficulties *sigh* (ASP.NET)
- Hi Access Date Problem (> Now +7 Days And < Now +2 Months) (Computer Science)
- !Help! spyware/virus problem! (Viruses, Spyware and other Nasties)
- Persistent Virus/Malware problem. (Viruses, Spyware and other Nasties)
- requiredfieldvalidator problem (ASP.NET)
- Have a ")" problem I can't solve (Java)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Help with Drop down list
- Next Thread: Password encoding
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array beta box browser captchaformproblem cart child close codes column css date debugger decimal dependent design disablefirebug dom download element embed engine enter error events ext file firefox focus form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index java javascript javascripthelp2020 jquery jsp jump libcurl listbox maps masterpage math media menu microsoft mimic mp4 onerror onmouseoutdivproblem onmouseover onreadystatechange paypal pdf php position post problem programming prototype rated rating redirect safari scale scriptlets scroll search security select software star starrating synchronous toggle unicode variables w3c web webservice window windowofwords xml \n





