0

I'm writing a javascript program and I need to find the best way to match string against the beginning of regex. My exact problem is to validate starting symbols of date in dd.mm.yyyy format. For example, 31.1 and 31.12.201 would be valid but 31.. or 31.122 would not.

I do not need regex specifically - if there is an easier way to do this in javascript, fine.

EDIT. Note, that i'm looking for a match of uncomplete date which is not what's been asked here.

Community
  • 1
  • 1
  • 1
    possible duplicate of [Javascript date regex DD/MM/YYYY](http://stackoverflow.com/questions/5465375/javascript-date-regex-dd-mm-yyyy) – ndnenkov Sep 28 '15 at 13:29

4 Answers4

2

You can do something like this

function validate(ele) {
  ele.style.color = /^(\d|\d{2}(\.\d?|(\.\d{2}(\.|\.\d{1,4})?)?))$$/.test(ele.value) ? 'green' : 'red';
}
<input type=text id=date oninput="validate(this)">

Regex explanation

Regular expression visualization

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

This does what you want:

mystring.match('^' + teststring) !== null

Where mystring would be 31.12.201 and teststring would be 31.1

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
0

You can do this to match string as well :

function validStr(str)
{
    if(str.length > 10 ) //if length is more than valid char its invalid
        return false;
    var patt=/\d{2}\.\d{2}\.\d{3}/g; //use regex to match pattern like 31.12.201 digit twice.digit twice.and digit  3 or 4 times at last
    return patt.test(str);
}
alert(validStr("31.12.201"));
Amit.S
  • 431
  • 2
  • 9
0

To validate a date in the format of dd.mm.yyyy with javascript you can use:

re = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;

valid 11/11/2015
invalid 13/13/2015

You can also use Jquery datePicker and validate with a regex, i.e.:

$("#datepicker").datepicker(
{
    onSelect: function()
    { 
        //var dateObject = $(this).datepicker('getDate'); 
      
      var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
      console.log(date);
      
      if (/(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/.test(date)) {
 alert(date+" VALID DATE !")
} else {
 alert(date+" INVALID DATE !")
}

      
    }
});

//if the user tries to edit the field manually we check for any changes 

  $( "#datepicker" ).change(function() {
  var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
      console.log(date);
      
      if (/(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/.test(date)) {
 alert(date+" VALID DATE !")
} else {
 alert(date+" INVALID DATE !")
}
});
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
 
 
</body>
</html>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268