Regular expression search not working

Discussion in 'Javascript' started by donpro, Aug 12, 2011.

  1. donpro

    donpro Guest

    Hi,

    I have a TEXTAREA where I wish to ensure that it contains at least on
    4 digit number. The following code snippet always returns -1 even when
    the 4 digit number is present and I don;t know why. Note that I first
    ensure that the vale is not empty but I am not showing that code in
    the snippet below.

    var hsCodeElement = document.getElementById('TaField));
    var hsCodePattern = '/^\d{4}$/';

    if (hsCodeElement.value.search(hsCodePattern) == -1) {
    alert(hsCodeElement.value.search(hsCodePattern) + ": HS Codes must
    contain at least entered one 4 digit numeric value");
    setTimeout(function() {hsCodeElement.focus();}, 10);
    }
     
    donpro, Aug 12, 2011
    #1
    1. Advertising

  2. donpro wrote:

    > I have a TEXTAREA where I wish to ensure that it contains at least on
    > 4 digit number. The following code snippet always returns -1 even when
    > the 4 digit number is present and I don;t know why. Note that I first
    > ensure that the vale is not empty but I am not showing that code in
    > the snippet below.
    >
    > var hsCodeElement = document.getElementById('TaField));
    > var hsCodePattern = '/^\d{4}$/';


    Drop the single quotes, a regular expression literal in Javascript s
    delimited by slashes i.e.
    var hsCodePattern = /^\d{4}$/;
    I am not sure however you want the achors ^ and $ as you say "contains
    at least on[sic] 4 digit number" while the expression you have written
    basically says "is exactly a 4 digit sequence" and contains nothing
    before or after that.


    > if (hsCodeElement.value.search(hsCodePattern) == -1) {


    if (hsCodePattern.test(hsCodeElement.value)) {

    seems easier.


    --

    Martin Honnen --- MVP Data Platform Development
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Aug 12, 2011
    #2
    1. Advertising

  3. On Fri, 12 Aug 2011 09:48:12 -0700, donpro wrote:

    > I have a TEXTAREA where I wish to ensure that it contains at least on 4
    > digit number.


    > var hsCodePattern = '/^\d{4}$/';


    If you mean "at least one sequence of 4 digits"

    var hsCodePattern = /\d{4}/;

    If you mean "at least one sequence of exactly 4 digits"

    var hsCodePattern = /(^|\s)\d{4}(\s|$)/;

    a) Don't quote the regex, it's a regex, not a string containing a regex.
    b) If you include start and end anchors, your pattern is treated more
    like "consists of exactly 4 digits" rather than "contains a 4 digit
    sequence".

    This html will allow you to evaluate the contents of a text area against
    4 different "find 4 digit" regexes.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type"
    content="text/html; charset=utf-8">
    <meta name="MSSmartTagsPreventParsing" content="TRUE">
    <title>Test RegExes</title>
    <script type="text/javascript">
    var regExes = new Array (/^\d{4}$/, /(^|\s)\d{4}(\s|$)/,
    /\s\d{4}\s/, /\d{4}/);
    function dgebi(id)
    {
    return document.getElementById(id);
    }
    function testRegExes()
    {
    var ta = dgebi('theTextArea');
    var re, alert;
    for (re = 0; re < regExes.length; re++)
    {
    alert = dgebi("alert_" + re);
    if (ta.value.match(regExes[re]))
    {
    alert.style.backgroundColor = "#00ff00";
    }
    else
    {
    alert.style.backgroundColor = "#ff0000";
    }
    }
    }
    </script>
    <style type="text/css">
    p {margin: 20px auto 0 auto; text-align: center;}
    table {margin: 20px auto 0 auto; border-collapse: collapse;}
    th, td {text-align: center;border: thin solid black;}
    </style>
    </head>
    <body onload="testRegExes()">
    <p>
    <textarea cols="60" rows="6" id="theTextArea"></textarea><br>
    <input type="button" value="Evaluate" onclick="testRegExes()">
    </p>
    <table width="80%">
    <tr>
    <td width="25%" id="alert_0">/^\d{4}$/</td>
    <td width="25%" id="alert_1">/(^|\s)\d{4}(\s|$)/</td>
    <td width="25%" id="alert_2">/\s\d{4}\s/</td>
    <td width="25%" id="alert_3">/\d{4}/</td>
    </tr>
    </table>
    <table width="30%">
    <tr>
    <th width="100%">Color Key</th>
    </tr>
    <tr>
    <td style="background-color: #00ff00">RegEx Matched</td>
    </tr>
    <tr>
    <td style="background-color: #ff0000">RegEx Not Matched</td>
    </tr>
    </table>
    </body>
    </html>

    Rgds

    Denis McMahon
     
    Denis McMahon, Aug 12, 2011
    #3
  4. 12.8.2011 21:06, Denis McMahon wrote:

    > If you mean "at least one sequence of exactly 4 digits"
    >
    > var hsCodePattern = /(^|\s)\d{4}(\s|$)/;


    That's really "at least one sequence of exactly 4 digits surrounded by
    whitespace or start or end of string"; for example, "a1234" does not
    match, and neither does "1234-5" or "1234.".

    If you want "at least one sequence of 4 digits that is not part of a
    longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Aug 12, 2011
    #4
  5. "Jukka K. Korpela" <> writes:

    > If you want "at least one sequence of 4 digits that is not part of a
    > longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.


    Or /\b\d{4}\b/

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Aug 12, 2011
    #5
  6. 12.8.2011 22:25, Lasse Reichstein Nielsen wrote:

    > "Jukka K. Korpela"<> writes:
    >
    >> If you want "at least one sequence of 4 digits that is not part of a
    >> longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.

    >
    > Or /\b\d{4}\b/


    No, that would be different; "a1234" would not match.


    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Aug 12, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,307
  2. Ratman
    Replies:
    0
    Views:
    665
    Ratman
    Sep 14, 2004
  3. CK
    Replies:
    3
    Views:
    9,123
  4. Replies:
    6
    Views:
    454
    John Harrison
    Feb 21, 2007
  5. Peter Hanke
    Replies:
    1
    Views:
    150
    Dr.Ruud
    Jan 6, 2008
Loading...

Share This Page