Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regular expression and javascript

Status
Not open for further replies.

RandyRiegel

Programmer
Sep 29, 2003
256
US
I am making a webpage for a user to scan a barcode into an input box (keyboard wedge reader). As soon as I get a good formatted job number I want to set focus to a hidden field which will start processing of data. But I can not get the regular expression to match, have tried all kinds of example online.

The format of our job numbers are 123456-1-1. First section is always 6 digits, the second section is ALWAYS 1 and the last section is 1-12 (for month) without leading zero. I got my regular expression to work in a windows application (vb.net) to test it. Here is my javascript/jquery code.

JavaScript:
    $(document).keyup(function () {
        var str = $("#<%= txtBarcode.ClientId %>").val();

        // $("#Hidden1").val(str);
        var re = /^\d{6}\-[1]\-[1-9][0-2]?$/.match(str);

        if (re == true) {
            $("#Hidden1").focus();
        }

    });

NOTE: str variable is getting a value, in the commented line above whatever I typed into the txtBarcode would get copied to the Hidden1 field (which I unhid for testing).

Thanks,
Randy
 
Hi

[tt]match()[/tt] is [tt]String[/tt] method. So either :
JavaScript:
[green][i]'123456-1-1'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/^\d{6}-1-(?:[1-9]|1[0-2])?$/[/fuchsia][teal])[/teal] [gray]// -> [ "123456-1-1" ][/gray]

[gray]// or[/gray]

[fuchsia]/^\d{6}-1-(?:[1-9]|1[0-2])?$/[/fuchsia][teal].[/teal][COLOR=darkgoldenrod]test[/color][teal]([/teal][green][i]'123456-1-1'[/i][/green][teal])[/teal]  [gray]// -> true[/gray]

[gray]// or[/gray]

[fuchsia]/^\d{6}-1-(?:[1-9]|1[0-2])?$/[/fuchsia][teal].[/teal][COLOR=darkgoldenrod]exec[/color][teal]([/teal][green][i]'123456-1-1'[/i][/green][teal])[/teal]  [gray]// -> [ "123456-1-1" ][/gray]

Note that I would not assigned any of those values to a variable called re, as re sounds like "regular expression", which may become misleading as none of the returned values are regular expressions. As there is no apparent need to store the value, I would just directly test it :
JavaScript:
$[teal]([/teal]document[teal]).[/teal][COLOR=darkgoldenrod]keyup[/color][teal]([/teal][b]function[/b] [teal]()[/teal] [teal]{[/teal]
    [b]var[/b] str [teal]=[/teal] $[teal]([/teal][green][i]"#<%= txtBarcode.ClientId %>"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]val[/color][teal]();[/teal]

    [b]if[/b] [teal]([/teal][fuchsia]/^\d{6}-1-(?:[1-9]|1[0-2])?$/[/fuchsia][teal].[/teal][COLOR=darkgoldenrod]test[/color][teal]([/teal]str[teal]))[/teal]
        $[teal]([/teal][green][i]"#Hidden1"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]focus[/color][teal]();[/teal]
[teal]}[/teal][teal]);[/teal]

Also note that you not have to escape the dash ( - ) in regular expressions unless used literally in a character class. Also no need to put a single literal character like that "1" a character class.

One last thing to note, your original expression accepted for example "123456-1-21", so I modified it abit the month part. But regular expressions are not suitable for numeric range checks, for anything more complex better extract that part an test it with numeric operators.

Feherke.
[link feherke.github.com/][/url]
 
Thanks for you help. For years I've been in desktop application development and the company I started working for now uses a lot of web based apps internally. So I haven't had much experience with Javascript but I'm learning. :)

Randy

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top