Jump to content

how to validate and format string using RegExp?

Recommended Posts



i'm reading back and forward but not really coming t a solution.


I would like to grab the content of an input field (not inside a form) i am using for date, but just as string.


the needed output format is always DD/MM/YYYY


so said :


12.02.16 or 2016

12.2.16 or 2016



and so on should all result in 12/02/2016


if there would be a timestamp behind it just needs to be cutted off.


I really thought this would be easier :-) But it isn't


Maybe somebody could point out a good article or solution for me.





I'm really struggling with the regular expressions :-)

  var txt='31-12/2016';

      var re1='(\\d)';	// Any Single Digit 1
      var re2='(\\d)';	// Any Single Digit 2
      var re3='(.)';	// Any Single Character 3
      var re4='(\\d)';	// Any Single Digit 1
      var re5='(\\d)';	// Any Single Digit 2
      var re6='(.)';	// Any Single Character 4
      var re7='((??:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])';	// Year 1

      var p = new RegExp(re1+re2+re3+re4+re5+re6+re7,["i"]);
      var m = p.exec(txt);
      if (m !== null)
          var c1=m[1];
          var c2=m[2];
          var c3="/";
          var d1=m[4];
          var d2=m[5];
          var c4="/";
          var year1=m[7];
alert("Please check format");
Edited by antidot
Link to post
Share on other sites

Use substring to look at 3rd character from end if '.' or '-' , you will know its the wrong format so adjust it to your needs

<!DOCTYPE html>

<p>Click the button to extract characters from the string.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

function myFunction() {
    var str = "12.02.16";


    var res = str.substring(str.length-3, str.length);
    if(str.substring(str.length-3, str.length-2) == "." || str.substring(str.length-3, str.length-2)==="-")
    tmp = str.substring(0, str.length-3)+'/20'+str.substring(str.length-2, str.length)
    tmp = tmp.replace('-','/')
    tmp = tmp.replace('.','/')
    document.getElementById("demo").innerHTML = res;


You will need same manipulation to check the middle digits (and first 2 digits me thinks) and add '0'.

  • Like 1
Link to post
Share on other sites

This is the regular expression you're building:


For one, you're requiring 2 digits for each of the month and day, so a 1-digit month or day isn't going to work. You're also allowing any character to separate things, so this is going to match:




It's going to parse that as 12/45/1234. You might want to define a set of characters that can be delimiters.


If you want to match 1 or 2 digits you can use "\d{1,2}".

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...