Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. regex help

regex help

Scheduled Pinned Locked Moved JavaScript
regexquestionjavascripthtmlsysadmin
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Wombaticus

    I want to force users to enter dates in a textbox as mm/yyyy The following works:

    <html>
    <head>
    <title></title>
    <script type="text/javascript" >
    function testFormat(v) {
    //var re = new RegExp("^([1-9]|1[0-2]|0[1-9])\/(\d{4})$");
    var re = new RegExp(document.getElementById("txtR").value);
    if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
    }
    </script>
    </head>
    <body ><form runat="server" id="Form1">
    <input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])\/(\d{4})$" style="width:200px;" />
    <br />
    <input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
    </form></body>
    </html>

    However: 1. Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft. 2. In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...

    T Offline
    T Offline
    thoiness
    wrote on last edited by
    #4

    Try this:

    <script type="text/javascript">
    function testFormat(v) {
    var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}([2-9]\d[1-9]\d|[1-9]\d)$/);
    if (v.match(re)) { alert('ok'); } else { alert('oops'); }
    }
    </script>

    Should also take care of your "7" problem

    W 2 Replies Last reply
    0
    • T thoiness

      Try this:

      <script type="text/javascript">
      function testFormat(v) {
      var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}([2-9]\d[1-9]\d|[1-9]\d)$/);
      if (v.match(re)) { alert('ok'); } else { alert('oops'); }
      }
      </script>

      Should also take care of your "7" problem

      W Offline
      W Offline
      Wombaticus
      wrote on last edited by
      #5

      Consider yourself raised to "Hero" status! :thumbsup: IDK how anyone gets their head round regular expressions. I don't pretend to be a genius, but I like to think I'm reasonably intelligent, but regex's defeat me. I'd have more chance solving a Rubik's cube blindfold. Thank you.

      1 Reply Last reply
      0
      • W Wombaticus

        I want to force users to enter dates in a textbox as mm/yyyy The following works:

        <html>
        <head>
        <title></title>
        <script type="text/javascript" >
        function testFormat(v) {
        //var re = new RegExp("^([1-9]|1[0-2]|0[1-9])\/(\d{4})$");
        var re = new RegExp(document.getElementById("txtR").value);
        if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
        }
        </script>
        </head>
        <body ><form runat="server" id="Form1">
        <input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])\/(\d{4})$" style="width:200px;" />
        <br />
        <input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
        </form></body>
        </html>

        However: 1. Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft. 2. In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #6

        Check here: http://regexper.com/#%5E(%5B1-9%5D%7C1%5B0-2%5D%7C0%5B1-9%5D)%2F(%5Cd%7B4%7D)%24[^] You have a lonely / in your regex that causes a syntax error... (Use this site/tool to visually check your regex in the future :-))

        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        W 1 Reply Last reply
        0
        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

          Check here: http://regexper.com/#%5E(%5B1-9%5D%7C1%5B0-2%5D%7C0%5B1-9%5D)%2F(%5Cd%7B4%7D)%24[^] You have a lonely / in your regex that causes a syntax error... (Use this site/tool to visually check your regex in the future :-))

          Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

          W Offline
          W Offline
          Wombaticus
          wrote on last edited by
          #7

          Ta - Not being funny, but you didn't have a grandad (?) who was a Latin teacher by any chance did you? I had a Peter Kornfield teaching that to me way back in the 60's/70's..... (least, I'm pretty sure it was 'Peter'.) He was an alright bloke...

          Kornfeld Eliyahu PeterK 1 Reply Last reply
          0
          • W Wombaticus

            Ta - Not being funny, but you didn't have a grandad (?) who was a Latin teacher by any chance did you? I had a Peter Kornfield teaching that to me way back in the 60's/70's..... (least, I'm pretty sure it was 'Peter'.) He was an alright bloke...

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #8

            I definitely had a grandfather :-D , but he was a travelling salesman of textiles...However I'm not sure he ever got to the UK, and in the 60s/70s he was clsoe to 60/70 as he was born in 1904... (What funny is that as a travelling salesman he spoke 7 languages - not including Latin, which he has learned in school but never used)

            Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            W 1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              I definitely had a grandfather :-D , but he was a travelling salesman of textiles...However I'm not sure he ever got to the UK, and in the 60s/70s he was clsoe to 60/70 as he was born in 1904... (What funny is that as a travelling salesman he spoke 7 languages - not including Latin, which he has learned in school but never used)

              Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

              W Offline
              W Offline
              Wombaticus
              wrote on last edited by
              #9

              Yeah, well I had two!! ;P OK, not him then. He was interesting - he had a gammy leg as a resuilt of some Nazi 'experiments' which he was unfortunate enough to be caught up in, but fortunate enough to survive. IDK the full story, being only a schoolboy at the time. But he had to spray some kind of medication on it even all these years later which used to stink to high heaven. But we all got used to it. He actually died in my last year there. Never forget his lessons, though can't say I've had much use for Latin either! Suppose it's sort of useful sometimes thinkng about the etymology of words....

              Kornfeld Eliyahu PeterK 1 Reply Last reply
              0
              • W Wombaticus

                Yeah, well I had two!! ;P OK, not him then. He was interesting - he had a gammy leg as a resuilt of some Nazi 'experiments' which he was unfortunate enough to be caught up in, but fortunate enough to survive. IDK the full story, being only a schoolboy at the time. But he had to spray some kind of medication on it even all these years later which used to stink to high heaven. But we all got used to it. He actually died in my last year there. Never forget his lessons, though can't say I've had much use for Latin either! Suppose it's sort of useful sometimes thinkng about the etymology of words....

                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu Peter
                wrote on last edited by
                #10

                Wombaticus wrote:

                resuilt of some Nazi 'experiments'

                My grandfather too survived one of those horrible things, they called it Auschwitz...

                Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                1 Reply Last reply
                0
                • W Wombaticus

                  I want to force users to enter dates in a textbox as mm/yyyy The following works:

                  <html>
                  <head>
                  <title></title>
                  <script type="text/javascript" >
                  function testFormat(v) {
                  //var re = new RegExp("^([1-9]|1[0-2]|0[1-9])\/(\d{4})$");
                  var re = new RegExp(document.getElementById("txtR").value);
                  if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
                  }
                  </script>
                  </head>
                  <body ><form runat="server" id="Form1">
                  <input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])\/(\d{4})$" style="width:200px;" />
                  <br />
                  <input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
                  </form></body>
                  </html>

                  However: 1. Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft. 2. In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #11

                  Although you've got a solution, I can't obviously see that anyone's given you an explanation why the commented out line doesn't work. :)

                  var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\d{4})$");

                  In Javascript strings[^], the backslash (\) is used to escape the following character. As a result, your pattern actually comes out as:

                  ^([1-9]|1[0-2]|0[1-9])/(d{4})$

                  That's looking for the literal character "d", not the digits character class "\d". You can solve it by either escaping the backslash within the string:

                  var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\\d{4})$");

                  or using a regular expression literal:

                  var re = /^([1-9]|1[0-2]|0[1-9])\/(\d{4})$/;

                  NB: For a regular expression literal you have to escape the forward-slash (/) character, since that's also used to terminate the literal.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  W 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Although you've got a solution, I can't obviously see that anyone's given you an explanation why the commented out line doesn't work. :)

                    var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\d{4})$");

                    In Javascript strings[^], the backslash (\) is used to escape the following character. As a result, your pattern actually comes out as:

                    ^([1-9]|1[0-2]|0[1-9])/(d{4})$

                    That's looking for the literal character "d", not the digits character class "\d". You can solve it by either escaping the backslash within the string:

                    var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\\d{4})$");

                    or using a regular expression literal:

                    var re = /^([1-9]|1[0-2]|0[1-9])\/(\d{4})$/;

                    NB: For a regular expression literal you have to escape the forward-slash (/) character, since that's also used to terminate the literal.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    W Offline
                    W Offline
                    Wombaticus
                    wrote on last edited by
                    #12

                    Thank you!

                    1 Reply Last reply
                    0
                    • T thoiness

                      Try this:

                      <script type="text/javascript">
                      function testFormat(v) {
                      var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}([2-9]\d[1-9]\d|[1-9]\d)$/);
                      if (v.match(re)) { alert('ok'); } else { alert('oops'); }
                      }
                      </script>

                      Should also take care of your "7" problem

                      W Offline
                      W Offline
                      Wombaticus
                      wrote on last edited by
                      #13

                      Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?

                      T 2 Replies Last reply
                      0
                      • W Wombaticus

                        Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?

                        T Offline
                        T Offline
                        thoiness
                        wrote on last edited by
                        #14

                        for 12/1995, you see where it says: [2-9] - change it to [1-9] It was assuming 2000+ 12/2000 - just a sec... Let me check it out...

                        W 1 Reply Last reply
                        0
                        • W Wombaticus

                          Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?

                          T Offline
                          T Offline
                          thoiness
                          wrote on last edited by
                          #15

                          Less restrictive:

                          <script type="text/javascript">
                          function testFormat(v) {
                          var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}(\d{4})$/);
                          if (v.match(re)) { alert('ok'); } else { alert('oops'); }
                          }

                          </script>
                          
                          W 1 Reply Last reply
                          0
                          • T thoiness

                            for 12/1995, you see where it says: [2-9] - change it to [1-9] It was assuming 2000+ 12/2000 - just a sec... Let me check it out...

                            W Offline
                            W Offline
                            Wombaticus
                            wrote on last edited by
                            #16

                            Ta - that bit works.. For the other - it doesn't seem to like a double-0 ...

                            1 Reply Last reply
                            0
                            • T thoiness

                              Less restrictive:

                              <script type="text/javascript">
                              function testFormat(v) {
                              var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}(\d{4})$/);
                              if (v.match(re)) { alert('ok'); } else { alert('oops'); }
                              }

                              </script>
                              
                              W Offline
                              W Offline
                              Wombaticus
                              wrote on last edited by
                              #17

                              :thumbsup: Thanks ever so

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups