Reputation: 1111
I am trying to do date validation.
My requirement is user should not be able to select past date and time. It works fine until I change my local PC's date and time.
I am comparing user selected date and time with new Date()
object of javascript but found that it fetches date from local machine's date. So if user changes his local machines date(set it to past date) then he would be able to select past dates.
Any server side(Java) solution is also acceptable. Try that too but Java's date object(java.util.date) also fetches date from local machine's date so no luck.
Thanks.
Upvotes: 0
Views: 647
Reputation: 3127
For that you should get date and time from server.
serverDateTime.js
var xmlHttp;
function srvTime(){
try {
//FF, Opera, Safari, Chrome
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//IE
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}
var st = srvTime();
var date = new Date(st);
Html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server date/time</title>
<script language="javascript" src="serverDateTime.js"></script>
</head>
<script language="javascript">
var localTime = new Date();
document.write("Local machine time is: " + localTime + "<br>");
document.write("Server time is: " + date);
</script>
<body>
</body>
</html>
Hope it will work. Source : webdeveloper
Upvotes: 1