TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anil Kumar
650
1.3k
133.2k
Javascript validation not working
Dec 16 2015 2:34 AM
I have three dropdown for day,month,year
There are two dates on is DateFrom and other is DateTo,
i want to use validate for DateTo not should is greater Then Date From
and day should be 28 for feb and 29 for Leap Year.
here is my code please check
Validation Script
<script type="text/javascript">
function validate()
{
var dateFrom = $('#ddd').val() + '-' + $('#ddm').val() + '-' + $('#ddy').val();
var dateTo = $('#dd').val() + '-' + $('#mm').val() + '-' + $('#yy').val();
var checkAlert = "true";
var dateTimeValue = dateFrom;
var dateTimeToValue = dateTo;
var dateDataFrom = dateTimeValue.split("-");
var dayFrom = (dateDataFrom[0]);
var monthFrom = (dateDataFrom[1]);
var yearFrom = (dateDataFrom[2]);
var dateDataTo = dateTimeToValue.split("-");
var dayTo = (dateDataTo[0]);
var monthTo = (dateDataTo[1]);
var yearTo = (dateDataTo[2]);
if (dateTimeValue != "" && dateTimeToValue != "") {
if (dateTimeValue > dateTimeToValue) {
alert("DateFrom can not greater then DateTo.");
return false;
}
else if (dateTimeValue.indexOf("_") > -1 || dateTimeValue.length < 10) {
alert("Please enter valid date in (dd-mm-yyyy) format.");
document.getElementById("dateFrom").value = "";
return false;
}
else if (dateTimeToValue.indexOf("_") > -1 || dateTimeValue.length < 10) {
alert("Please enter valid date in (dd-mm-yyyy) format.");
document.getElementById("dateTo").value = "";
return false;
}
else if (!validateDateTime(dateTimeValue)) {
alert("Please enter valid date. in DateFrom");
document.getElementById("dateFrom").value = "";
document.getElementById("dateTo").value = "";
return false;
}
else if (!validateDateTime(dateTimeToValue)) {
alert("Please enter valid date. in DateTo");
document.getElementById("dateTo").value = "";
document.getElementById("dateFrom").value = "";
return false;
}
else if (yearFrom > yearTo) {
alert('from year is greater than to year');
document.getElementById("dateFrom").value = "";
document.getElementById("dateTo").value = "";
}
else if (yearFrom < yearTo || yearFrom == yearTo) {
if (monthFrom > monthTo) {
alert('Enter Month Correctly');
document.getElementById("dateFrom").value = "";
document.getElementById("dateTo").value = "";
return false;
}
}
else if (yearFrom < yearTo || yearFrom == yearTo) {
if (monthFrom < monthTo || monthFrom == monthTo) {
if (dayFrom > dayTo) {
alert('Enter Date Correctly');
document.getElementById("dateFrom").value = "";
document.getElementById("dateTo").value = "";
return false;
}
}
}
}
function validateDateTime(date) {
var valid = true;
var dateData = date.split("-");
var day = (dateData[0]);
var month = (dateData[1]);
var year = (dateData[2]);
var regForDate = /^[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2}$/;
if (!date.match(regForDate)) {
valid = false;
}
else if ((month < 1) || (month > 12)) valid = false;
else if ((day < 1) || (day > 31)) valid = false;
else if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) valid = false;
else if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) && (day > 29)) valid = false;
else if ((month == 2) && ((year % 100) == 0) && (day > 29)) valid = false;
return false;
}
return true;
}
</script>
.aspx page:-
<label for="dateFrom">Date From</label>
<SELECT class="combine" id ="ddd" name = "dd"></SELECT>
<SELECT class="combine" id ="ddm" name = "mm"></SELECT>
<SELECT class="combine" id ="ddy" name = "yyyy" ></SELECT>
<%-- <input type="text" name="dateFrom" id="dateFrom" class="select1" style=" margin-left: 34px;" /> --%>
<label for="dateTo">Date To</label>
<SELECT class="combine" id ="dd" name = "dd"></SELECT>
<SELECT class="combine" id ="mm" name = "mm"></SELECT>
<SELECT class="combine" id ="yy" name = "yyyy" ></SELECT>
<%-- <input type="text" name="dateTo" id="dateTo" class="select1" /> --%>
</div>
<input type="submit" name="buttonView" id="buttonViewReport" value="VIEW" onclick="validate();" class=" btn bt"/><br />
Thanks
Reply
Answers (
2
)
How do we use SPA[Single Page Application] in ASP .Net MVC?
Data annotation in c#