Jump to content

how to validate and format string using RegExp?


antidot

Recommended Posts

Hi,

 

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

12-02-16

 

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.

 

Thx.

 

 

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(c1+c2+c3+d1+d2+c4+year1+"\n");
      }
else
{
alert("Please check format");
}
Edited by antidot
Link to comment
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>
<html>
<body>

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

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

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

<script>
function myFunction() {
    var str = "12.02.16";

//12.2.16

//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('.','/')
    res=tmp;
    }
    
  
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

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

  • Like 1
Link to comment
Share on other sites

This is the regular expression you're building:

 

(\\d)(\\d)(.)(\\d)(\\d)(.)((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])
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:

 

1234561234X

 

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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...