Skip to content Skip to sidebar Skip to footer

Date Format Validation With Jquery

I am trying to validate the date format (from datepicker) If the date format is right then go to next step if wrong then alert if ($('#date').val() != (/^\d{2}\/\d{2}\/\d{4}$/)) {

Solution 1:

Try:

if (!(/^\d{2}\/\d{2}\/\d{4}$/).test($("#date").val())) {
  $("#date").css('background-color', '#FF0000');
}
else {
  alert("good");
}

Solution 2:

you should try :

if (!/^\d{2}\/\d{2}\/\d{4}$/.test($("#date").val())) {
  $("#date").css('background-color', '#FF0000');
}
else {
  alert("good");
}

You can't compare a string to a RegExp object with == or != operators, you have to use Regexp methods. see mdn

Solution 3:

Look at this code helps:

functioncall()
{
    if ($("#date").val().search((/^\d{2}\/\d{2}\/\d{4}$/))>-1) {
        $("#date").css('background-color', '#FF0000');
    }
    else {
        alert("good");
    }
}

Hope this helps and answers your question and also for this thread. :)

Post a Comment for "Date Format Validation With Jquery"