1

How to check entered date format is valid or not in text box $("#txtCustomDate").

function datVal(){
  var obj = $("#txtCustomDate").val();
  var format = obj.toUpperCase();
  var date = new Date();
  text = moment(date).format(format);
  $('#editor').val(value + "<span>" + text + "</span>");
}
jayesh patil
  • 63
  • 14
  • here is the solution please check... [Valid Date Format](http://stackoverflow.com/a/8098359/4018240) – Manjeet Barnala Mar 17 '16 at 11:56
  • @ManjeetBarnala I think the question is to check if provided string is a valid date format pattern, not if it is a date in valid format, i.e. the string `"mm-dd-yyyy"` makes sense as a date format, but `"mmm-uu-abc"` does not. – pawel Mar 17 '16 at 12:00
  • The idea to try and format a date using the provided string makes sense. Problem is moment.js will just print the tokens it doesn't recognize as formatting patterns, for example `moment().format("WTF MAN")` will happily substitute the `W` for week number, leave the `TF` as is, then `3` for `M` (month), `PM` for `A` (AM/PM), then `N` as-is. So you'll get something like `"11TF 3PMN"` because the format pattern was kind of valid, at least for the library. – pawel Mar 17 '16 at 12:11
  • Your question should include how you've attempted to solve the issue, what you expect as output and what you are getting, along with any error messages. Whether a date format string is valid or not depends entirely on the tokens that your parser recognises. – RobG Mar 17 '16 at 12:34
  • First of all you need to narrow down what you consider valid and invalid pattern. – pawel Mar 17 '16 at 12:52

1 Answers1

-1

var dateExp= /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
if(dateExp.test('17-03-2016'))
{
 alert('Valid Format');
}
else
{
 alert('Invalid Format');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
  • Any explanation? How would you use it to validate date format strings? – pawel Mar 17 '16 at 12:14
  • 1
    Code-only answers are not helpful, you should explain the OP's issue, how to fix it and how your code can help. – RobG Mar 17 '16 at 12:32
  • Suppose User enter DD/MMMMMMMMM/Y ,then it is wrong format,so it should give error. – jayesh patil Mar 17 '16 at 12:36
  • Dear @pawel please visit [Link1](http://bit.ly/22nhtFd) and [Link2](http://regexr.com/3d1ek) for details. – Manjeet Barnala Mar 17 '16 at 12:40
  • @jayeshpatil well, is it really wrong? moment.js won't reject it, it will print `""17/MarchMarch3/2016"` which is maybe stupid, but not technically wrong. – pawel Mar 17 '16 at 12:41
  • @ManjeetBarnala yeah I get what this regex does, but it doesn't apply to this question (which is not about dates, but date format expressions). Or if it does you should include an explanation in your answer. – pawel Mar 17 '16 at 12:43