Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by mrong
at 2024-07-30 11:14:10
Point:500 Replies:40 POST_ID:828640USER_ID:11511
Topic:
JavaScript;Active Server Pages (ASP);Scripting Languages
Greeting,
I have asp form which contains text box MyDate in "30-JUL-2013" format. How to add a validation so MyDate >= currentDate+2days?
something like the following
if(document.frmUser.MyDate.value> ???????)
{
alert("Date can't be less than 2 days from the now.");
return false;
}
thanks.
I have asp form which contains text box MyDate in "30-JUL-2013" format. How to add a validation so MyDate >= currentDate+2days?
something like the following
if(document.frmUser.MyDate.value> ???????)
{
alert("Date can't be less than 2 days from the now.");
return false;
}
thanks.
Author: mrong replied at 2024-07-31 09:28:14
Thank you duncanb7
Expert: duncanb7 replied at 2024-07-31 09:25:05
Thanks, mrong and that code is also useful in future if you need date format "dd-mmm-yyyy hh:mm:ss" with just a little modification of split text command code
Author: mrong replied at 2024-07-31 09:22:51
Thanks!
Expert: duncanb7 replied at 2024-07-31 01:20:39
Complete and compact version is also working too with
date format, dd-mmm-yyyy (such 12-Aug-2013 or 12-Jul-2013),
please try it if need
date format, dd-mmm-yyyy (such 12-Aug-2013 or 12-Jul-2013),
please try it if need
function VerifyData(){var obj= document.frmUser.orderdate3;var d = new Date();var a= new Date(Date.parse(obj.value.split("-")[1] +" 1, 2012")).getMonth();var k=Date.UTC(obj.value.split("-")[2],a, obj.value.split("-")[0]);var m=Date.UTC(d.getFullYear(),d.getMonth(),d.getDate());if(k-m<2*86400*1000){ alert("The Event Start Date can't be less than 2 days");return false; }if(isWhitespace(document.frmUser.name.value,true)){alert("Requestor's Name is a required field.");return false; }else return true;} 1:2:3:4:5:6:7:8:9:
Expert: julianH replied at 2024-07-31 00:27:05
The script might have failed due to the console output (if you were using IE).
Here is a cleaned version of the script
Here is a cleaned version of the script
<!doctype html><html><head><title>Test</title><script type="text/javascript">function checkDates(obj){ var mydate = customParse(obj.value); var today = new Date(); var twodays = new Date(); twodays.setDate(today.getDate() + 2); if (mydate > twodays) alert('you are ok'); else alert('date is not right');}function customParse(str) { var months = ['JAN','FEB','MAR','APR','MAY','JUN', 'JUL','AUG','SEP','OCT','NOV','DEC'], n = months.length, re = /(d{2})-([a-z]{3})-(d{4})/i, matches; while(n--) { months[months[n]]=n; } // map month names to their index :) matches = str.match(re); // extract date parts from string var month = matches[2]; return new Date(matches[3], months[month.toUpperCase()], matches[1]);}</script></head><body><input type="text" name="MyDate" onblur="checkDates(this);"/></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:
Online version updated as well
Accepted Solution
Expert: duncanb7 replied at 2024-07-30 23:57:27
500 points EXCELLENT
mrong,please try the final version
I have already checked it that is working on my browser
and met all your need,
I have already checked it that is working on my browser
and met all your need,
function getMonth3(mon){ return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1}function VerifyData() { var obj= document.frmUser.orderdate3; var day = obj.value.split("-")[0]; var month = getMonth3(obj.value.split("-")[1]); var year = obj.value.split("-")[2]; var k=Date.UTC(year, month-1, day); var d = new Date(); ny=d.getFullYear(); nm=d.getMonth(); nd=d.getDate(); var m=Date.UTC(ny, nm, nd); if(k-m<2*86400*1000) { alert("The Event Start Date can't be less than 2 days"); return false; } if(isWhitespace(document.frmUser.name.value,true)) { alert("Requestor's Name is a required field."); return false; } else return true; } 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:
Expert: Michel Plungjan replied at 2024-07-30 23:50:33
Can we please clean up this?
function VerifyData() { if (isWhitespace(document.frmUser.name.value,true)) { alert("Requester name is a required field."); return false; } var dParts= document.frmUser.orderdate3.value.split("-"); if (dParts.length<3) { alert("Please enter the date in the required format"); return false; } var day = dParts[0]; var month = getMonth3(dParts[1]); var year = dParts[2]; var k=Date.UTC(year, month-1, day); var d = new Date(); var ny=d.getFullYear(), nm=d.getMonth(), nd=d.getDate() m=Date.UTC(ny, nm, nd); if(k-m<2*86400*1000) { alert("The Event Start Date can't be less than 2 days"); return false; } return true;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:
Expert: julianH replied at 2024-07-30 13:39:07
@mrong - yes it will!!!
Go to the link I posted and cut and paste those dates into the text box then hit tab to kick off the script.
The script works and is correct
Go to the link I posted and cut and paste those dates into the text box then hit tab to kick off the script.
The script works and is correct
Expert: duncanb7 replied at 2024-07-30 13:28:08
Just convert Jul to number by additional function of getMonth3() I give you
var month = getMonth3(obj.value.split("-")[1]) ;
Now it is final version, probably it is no issue , fix it tomorrow that should work
And UTC is also take care of hour,minutes, second in the future if you need
var month = getMonth3(obj.value.split("-")[1]) ;
Now it is final version, probably it is no issue , fix it tomorrow that should work
And UTC is also take care of hour,minutes, second in the future if you need
function getMonth3(mon){ return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1}function VerifyData() { var obj= document.frmUser.orderdate3; var day = obj.value.split("-")[0]; var month = getMonth3(obj.value.split("-")[1]); var year = obj.value.split("-")[2]; var k=Date.UTC(year, month-1, day); var d = new Date(); ny=d.getFullYear(); nm=d.getMonth(); nd=d.getDate(); var m=Date.UTC(ny, nm, nd); if(k-m<2*86400*1000) { alert("The Event Start Date can't be less than 2 days"); return false; } if(isWhitespace(document.frmUser.name.value,true)) { alert("Requestor's Name is a required field."); return false; } else return true; } 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:
Author: mrong replied at 2024-07-30 13:27:53
julianH,
on the sample you provided, it wont accept both 30-JUN-2013 & 02-AUG-2013.
Thanks.
on the sample you provided, it wont accept both 30-JUN-2013 & 02-AUG-2013.
Thanks.
Author: mrong replied at 2024-07-30 13:25:15
ok, I am leaving. will come back tomorrow morning.
thanks.
thanks.
Expert: julianH replied at 2024-07-30 13:25:02
Damn - can't leave this alone.
Please look at the code below - based on previous posts.
Please look at the code below - based on previous posts.
<!doctype html><html><head><title>Test</title><script type="text/javascript">function checkDates(obj){ var mydate = customParse(obj.value); var today = new Date(); var twodays = new Date(); twodays.setDate(today.getDate() + 2); if (mydate > twodays) alert('you are ok'); else alert('date is not right');}function customParse(str) { var months = ['JAN','FEB','MAR','APR','MAY','JUN', 'JUL','AUG','SEP','OCT','NOV','DEC'], n = months.length, re = /(d{2})-([a-z]{3})-(d{4})/i, matches; while(n--) { months[months[n]]=n; } // map month names to their index :) matches = str.match(re); // extract date parts from stringconsole.log(matches); var month = matches[2]; return new Date(matches[3], months[month.toUpperCase()], matches[1]);}</script></head><body><input type="text" name="MyDate" onblur="checkDates(this);"/></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:
Working sample here http://www.marcorpsa.com/ee/t335.html
Type in your date as you have it 30-JUN-2013 - It won't accept it
Type in 02-AUG-2013 - it will.
Expert: duncanb7 replied at 2024-07-30 13:16:11
Wait for second just Jul should convert number by getMonth()
Author: mrong replied at 2024-07-30 13:15:06
Yes, The condition should be (m-k<2*86400*1000).
Expert: duncanb7 replied at 2024-07-30 13:09:41
you changed k-m<2*86400*1000 from k-m>2*86400*1000 from my code
that anyway, just up to you for your condition.
that anyway, just up to you for your condition.
Author: mrong replied at 2024-07-30 13:07:19
alert(document.frmUser.orderdate3.value);
returns me 30-JUL-2013
returns me 30-JUL-2013
Author: mrong replied at 2024-07-30 13:04:32
Below is html code and my orderdate3 is a date picker.
<font color="#000080" face="Arial"><br>Event Date:</font><font color="#000080"> </font><font face="Arial" color="#BD0000">* <script>DateInput('orderdate3', true, 'DD-MON-YYYY')</script>
<font color="#000080" face="Arial"><br>Event Date:</font><font color="#000080"> </font><font face="Arial" color="#BD0000">* <script>DateInput('orderdate3', true, 'DD-MON-YYYY')</script>
Expert: duncanb7 replied at 2024-07-30 13:03:55
THere is no need for "else", just "return true", right ?
Author: mrong replied at 2024-07-30 13:02:48
I tried alert(m); and alert(k);
Both return undefined.
Both return undefined.
Expert: duncanb7 replied at 2024-07-30 12:58:48
Must be the form issue, please use more alert() for checking pt for debugging if need
please send me your form/input html code for checking for you
If {
}
If
{
}
else
{
}
THat is error, please review it
please send me your form/input html code for checking for you
If {
}
If
{
}
else
{
}
THat is error, please review it
Expert: julianH replied at 2024-07-30 12:57:10
Ok must be an invisible day for me - signing out - good luck
Expert: duncanb7 replied at 2024-07-30 12:57:07
Could you send your input box html code in the form
Author: mrong replied at 2024-07-30 12:55:27
duncanb7,
Below is what I changed to, but still not working.
function VerifyData()
{
var obj= document.frmUser.orderdate3;
var day = obj.value.split("-")[0];
var month = obj.value.split("-")[1];
var year = obj.value.split("-")[2];
var k=Date.UTC(year, month-1, day);
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
var m=Date.UTC(ny, nm, nd);
if(k-m<2*86400*1000)
{
alert("The Event Start Date can't be less than 2 days");
return false;
}
if(isWhitespace(document.frmUser.name.value,true))
{
alert("Requestor's Name is a required field.");
return false;
}
else
return true;
}
Below is what I changed to, but still not working.
function VerifyData()
{
var obj= document.frmUser.orderdate3;
var day = obj.value.split("-")[0];
var month = obj.value.split("-")[1];
var year = obj.value.split("-")[2];
var k=Date.UTC(year, month-1, day);
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
var m=Date.UTC(ny, nm, nd);
if(k-m<2*86400*1000)
{
alert("The Event Start Date can't be less than 2 days");
return false;
}
if(isWhitespace(document.frmUser.name.value,true))
{
alert("Requestor's Name is a required field.");
return false;
}
else
return true;
}
Expert: duncanb7 replied at 2024-07-30 12:53:21
More compact version
<html><body onload="check()";><form name="myform"><input name="mydate" value="30-7-2013"></form></body></htmL><script type="text/javascript">function check(){var obj= document.myform.mydate;var k=Date.UTC(obj.value.split("-")[2], obj.value.split("-")[1]-1, obj.value.split("-")[0]);var d = new Date();var m=Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());if (m-k>2*86400*1000){alert ("The input box data is far from today more than 2 days, please resubmit the form");}else{alert("Accepted");}} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:
Expert: duncanb7 replied at 2024-07-30 12:38:31
Final with your format, some code is fixed
One day=60*60*24*1000= 86400000 millsecond For UTC basis
so 2days=2*864000000 millsecond For UTC basis
IN UTC, month is from 0 -11
One day=60*60*24*1000= 86400000 millsecond For UTC basis
so 2days=2*864000000 millsecond For UTC basis
IN UTC, month is from 0 -11
<html><body onload="check()";><form name="myform"><input name="mydate" value="31-7-2013"></form></body></htmL><script type="text/javascript">function check(){var obj= document.myform.mydate;var day = obj.value.split("-")[0];var month = obj.value.split("-")[1];var year = obj.value.split("-")[2];var k=Date.UTC(year, month-1, day);var d = new Date();ny=d.getFullYear();nm=d.getMonth();nd=d.getDate();var m=Date.UTC(ny, nm, nd);//alert(year+"==="+(month-1)+"==="+day);//alert(ny+"==="+nm+"==="+nd);//alert(m+"==="+k+"==="+"==="+(m-k));if (m-k>2*86400*1000){alert ("The input box data is far from today more than 2 days, please resubmit the form");}else{alert("Accepted");}} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:
Expert: julianH replied at 2024-07-30 12:36:27
I have split these two points so you don't miss one
Look here
http://stackoverflow.com/questions/3509683/validate-two-dates-of-this-dd-mmm-yyyy-format-in-javascript
And here
http://stackoverflow.com/questions/3552461/how-to-format-javascript-date
On how to write a custom date parsing function for the date format you are using (I am assuming you are unable to change the format you are using - not sure why - would be easier but given that is the case then please read the above - the code for what you want is in those links).
Look here
http://stackoverflow.com/questions/3509683/validate-two-dates-of-this-dd-mmm-yyyy-format-in-javascript
And here
http://stackoverflow.com/questions/3552461/how-to-format-javascript-date
On how to write a custom date parsing function for the date format you are using (I am assuming you are unable to change the format you are using - not sure why - would be easier but given that is the case then please read the above - the code for what you want is in those links).
Expert: julianH replied at 2024-07-30 12:34:19
tried your suggestion as the following but didn't work
That's because you did not read my post.
Point b) pointed out that your source date format needs to change.
Expert: duncanb7 replied at 2024-07-30 12:32:32
Sorry, "/" should be replaced by "-" for dd-mm-yyyy format
var day = obj.value.split("-")[0];
var month = obj.value.split("-")[1];
var year = obj.value.split("-")[2];
http://www.w3schools.com/jsref/jsref_split.asp
var day = obj.value.split("-")[0];
var month = obj.value.split("-")[1];
var year = obj.value.split("-")[2];
http://www.w3schools.com/jsref/jsref_split.asp
Expert: duncanb7 replied at 2024-07-30 12:31:42
That is for my format 2013/7/12
Now for your format
var day = obj.value.split("/")[0];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[2];
Now for your format
var day = obj.value.split("/")[0];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[2];
Author: mrong replied at 2024-07-30 12:30:07
Mydate will always in the format of "dd-mmm-yyyy".
Tried the following but didn't work.
function VerifyData()
{
var obj= document.frmUser.MyDate;
var day = obj.value.split("/")[2];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[0];
var k=Date.UTC(year, month-1, day);
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
if(m-k>2*86400*1000 )
{
alert("Event Start Date can't be less than 48 hours.");
return false;
}
else
return true;
}
Tried the following but didn't work.
function VerifyData()
{
var obj= document.frmUser.MyDate;
var day = obj.value.split("/")[2];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[0];
var k=Date.UTC(year, month-1, day);
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
if(m-k>2*86400*1000 )
{
alert("Event Start Date can't be less than 48 hours.");
return false;
}
else
return true;
}
Expert: duncanb7 replied at 2024-07-30 12:23:59
<input name="mydate" value="2013/7/12">
The value can be changed by your client input for different date
Please see also for hour, min, sec if need
at http://www.w3schools.com/jsref/jsref_obj_date.asp
The value can be changed by your client input for different date
Please see also for hour, min, sec if need
at http://www.w3schools.com/jsref/jsref_obj_date.asp
Expert: insoftservice replied at 2024-07-30 12:21:40
var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();
if (x>today)
{
alert("Today is before 14th January 2100");
}
else
{
alert("Today is after 14th January 2100");
}
x.setFullYear(2100,0,14);
var today = new Date();
if (x>today)
{
alert("Today is before 14th January 2100");
}
else
{
alert("Today is after 14th January 2100");
}
Expert: duncanb7 replied at 2024-07-30 12:18:02
One day=60*60*24*1000= 86400000 millsecond For UTC basis
so 2dayss=2*864000000 millsecond For UTC basis
Please take a lot the complete example,
If you need you can also add back hour, minute, second , now
I just year, month, day only for caculation
<html>
<body onload="check()";>
<form name="myform">
<input name="mydate" value="2013/7/12">
</form>
</body>
</htmL>
<script type="text/javascript">
function check(){
var obj= document.myform.mydate;
var day = obj.value.split("/")[2];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[0];
var k=Date.UTC(year, month-1, day);
//Today now;
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
var m=Date.UTC(ny, nm, nd);
//alert(year+"==="+(month-1)+"==="+day);
//alert(ny+"==="+nm+"==="+nd);
//alert(m+"==="+k+"==="+"==="+(m-k));
if (m-k>2*86400*1000){
alert ("The input box data is far from today more than 2 days, please resubmit the form");
}
else{alert("accepted");}
}
</script>
Other stuff about form input , I think you can take care
Duncan
so 2dayss=2*864000000 millsecond For UTC basis
Please take a lot the complete example,
If you need you can also add back hour, minute, second , now
I just year, month, day only for caculation
<html>
<body onload="check()";>
<form name="myform">
<input name="mydate" value="2013/7/12">
</form>
</body>
</htmL>
<script type="text/javascript">
function check(){
var obj= document.myform.mydate;
var day = obj.value.split("/")[2];
var month = obj.value.split("/")[1];
var year = obj.value.split("/")[0];
var k=Date.UTC(year, month-1, day);
//Today now;
var d = new Date();
ny=d.getFullYear();
nm=d.getMonth();
nd=d.getDate();
var m=Date.UTC(ny, nm, nd);
//alert(year+"==="+(month-1)+"==="+day);
//alert(ny+"==="+nm+"==="+nd);
//alert(m+"==="+k+"==="+"==="+(m-k));
if (m-k>2*86400*1000){
alert ("The input box data is far from today more than 2 days, please resubmit the form");
}
else{alert("accepted");}
}
</script>
Other stuff about form input , I think you can take care
Duncan
Author: mrong replied at 2024-07-30 12:14:59
julianH,
tried your suggestion as the following but didn't work
var twodays = new Date();
twodays.setDate(today.getDate() + 2);
if(twoday < document.frmUser.MyDate.value )
{
alert("Event Start Date can't be less than 48 hours.");
return false;
}
tried your suggestion as the following but didn't work
var twodays = new Date();
twodays.setDate(today.getDate() + 2);
if(twoday < document.frmUser.MyDate.value )
{
alert("Event Start Date can't be less than 48 hours.");
return false;
}
Expert: julianH replied at 2024-07-30 12:11:46
Two things
a) The reason your code does not work is that you are trying to compare a javascript date object to a string - you first have to convert your document.frmUser.oMyDate.value to a Javascript Date objct
b) If your form element has the date in the format you describe in your question it won't work because the Date object won't recognise it - try putting it in the form
Month Day, Year Example Aug 2, 2013
a) The reason your code does not work is that you are trying to compare a javascript date object to a string - you first have to convert your document.frmUser.oMyDate.value to a Javascript Date objct
b) If your form element has the date in the format you describe in your question it won't work because the Date object won't recognise it - try putting it in the form
Month Day, Year Example Aug 2, 2013
Expert: julianH replied at 2024-07-30 12:09:28
This should do it
var x = new Date('Aug 2, 2013'); var today = new Date(); var twodays = new Date(); twodays.setDate(today.getDate() + 2); if (x < twodays) alert('Nada'); else alert('you ok'); 1:2:3:4:5:6:
Also be sure to read this to understand what date strings you can use
http://tools.ietf.org/html/rfc2822#page-14
Author: mrong replied at 2024-07-30 12:06:41
I have the followings but doesn't work. Any suggestions?
var someDate = new Date();
var numberOfDaysToAdd = 2;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
if(someDate < document.frmUser.oMyDate.value )
{
alert("can't be less than 48 hours.");
return false;
}
var someDate = new Date();
var numberOfDaysToAdd = 2;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
if(someDate < document.frmUser.oMyDate.value )
{
alert("can't be less than 48 hours.");
return false;
}
Author: mrong replied at 2024-07-30 11:58:12
I need samples. thanks
Expert: mcnute replied at 2024-07-30 11:57:35
Expert: insoftservice replied at 2024-07-30 11:53:37