3

I need to check the data format in dd-mm-yyyy format.

I'm using the following:

if (ValidateDate() == false) {
   alert("Wrong Date Format");
return false;
}

function ValidateDate() {

    var dtValue = $('#txtEnteredStartDate').val();
    var dtRegex = new RegExp("^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-]([JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC])[-](d{4}))$");
    return dtRegex.test(dtValue);
}

But when I enter dd-mmm-yyyy in the field, for example 19-Nov-2015, I'm getting the error Wrong date format

What am I doing wrong?

gene
  • 2,098
  • 7
  • 40
  • 98
  • @gene Is `'#txtEnteredStartDate'` an `input type="date"` element ? – guest271314 Nov 11 '15 at 19:19
  • Using just a regex is not sufficient, it will not validate for example number of days in different months. For example 31-Feb-2015 would pass, but no such date exists. You need to parse your date-string, create a Date object from it and compare if it is still the original date (e.g. 31 Feb would become 3 Mar - a different date) – przno Nov 11 '15 at 20:21
  • With the regex you can validate the format, not the value, that's what I wanted to express – przno Nov 11 '15 at 20:23
  • I'm using the calendar control and provide the logic to disallow entering the data into the input field except of "-". Also, I only allow to delete data by pressing backslash – gene Nov 11 '15 at 20:26

3 Answers3

2

PhilVarg's regex is correct, though applying it in RegExp you need to \\ the d.

Also, you can re-factor the ([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1]) down to ([0]?[1-9]|[1-2]\\d|3[0-1]) which means an option 0 followed by 1 to 9 or 1,2 followed by a number, or 3 followed by 0 or 1.

1 Last thing, theres no need for [] around the -, and to pass i (ignore case) RegExp accepts it as a second argument.

Working Example:-

function ValidateDate() {
  var dtValue = $('#txtEnteredStartDate').val();
  var dtRegex = new RegExp("^([0]?[1-9]|[1-2]\\d|3[0-1])-(JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC)-[1-2]\\d{3}$", 'i');
  return dtRegex.test(dtValue);
}

$('.check').click(function() {
  if (ValidateDate() == false) {
    $('.result').html("Wrong Date Format");
  } else {
    $('.result').html("Right Date Format");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtEnteredStartDate" value="19-Nov-2015" />
<input type="button" class="check" value="check" />
<span class="result"></span>
BenG
  • 14,826
  • 5
  • 45
  • 60
0

you have a couple of errors in your regex

first, (d{4}) needs to be (\d{4}) because its currently looking for 4 d's and needs to look for four digits

second, the brackets around the months are causing it to fail as it looking for a single character J, A, or N OR F, E, B etc

and last, it needs to be a case insensitive regex

the following regex should work:

/^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-](JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC)[-](\d{4}))$/i

as seen here

PhilVarg
  • 4,762
  • 2
  • 19
  • 37
0

I found another way that worked for me:

function ValidateDate(dtValue) {

    var dtRegex = new RegExp(/^\d\d?-\w\w\w-\d\d\d\d/);
    return dtRegex.test(dtValue);
}

Thank you all for your suggestions

gene
  • 2,098
  • 7
  • 40
  • 98
  • be carefully with that as it would match `00-___-0000`. `\w` matchs letters, numbers and underscore. – BenG Nov 11 '15 at 20:23
  • In my case, I'm using the calendar control and provide the logic to disallow entering the data into the input field except of "-". Also, I only allow to delete data by pressing backslash – gene Nov 11 '15 at 20:27
  • 1
    @gene, You're missing a `$` at the end. @BG101, How did you guess my password? – Mike Samuel Nov 11 '15 at 20:32