How to compare two textboxes in jquery.

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
t1 and t2 are two be compared.
The type of values in t1 and t2 are dates.
t2 should be greater then t1
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if( (new Date(t1).getTime() >= new Date(t2).getTime()))
{
    alert('t2 should be greater then t1');
    return false;
}
You can also use jquery validator and your own custom validator to it.

jQuery.validator.addMethod("greaterThan",
function(value, element, params) {

    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date($(params).val());
    }

    return isNaN(value) && isNaN($(params).val())
        || (parseFloat(value) > parseFloat($(params).val()));
},'Must be greater than {0}.');


Use:

$("form").validate({
    rules: {
        EndDate: { greaterThan: "#StartDate" }
    }
});

For more info:
http://stackoverflow.com/questions/833997/end-date-greater-than-start-date-jquery-validation
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Anuradha GoliSystems Development / Support Specialist

Commented:
If you are also using jQuery ui, in particular datepicker, you can use $.datepicker.parseDate(format, string) to turn your date strings into a JavaScript Date object, which you can then compare using the standard < and >

Author

Commented:
<script type="text/javascript">
     $(document).ready(function () {
         $.validator.addMethod("txttdate", function (value, element) {
             var startDate = $('.txtfdate').val();
             return Date.parse(startDate) <= Date.parse(value) || value == "";
         }, "* End date must be after start date");
         $('#form1').validate();
     });
</script>

Not working

Author

Commented:
ok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial