Richard
Richard

Reputation: 826

How To Create A RegEx in Javascript to Validate an Inputbox

I haven't done one before and I need to create a RegEx to validate an input box. # represents a numeric and X represents a letter. The format is:

11111 - XXXXXXX

or

11111 - XXXXXXX-XXXXXXXX XXXX

The inputbox has an autocomplete that is pulling from the database and returns a list that games with an ID followed by the organization name.

Such as:

Basically, the numbers before the first - are the ID and anything following is the name of the organization that can possibly have a - inside of it.

How do I handle it to where it can allow for both possible entries?

Any help would be greatly appreciated for the regex and/or resources!

Upvotes: 0

Views: 802

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

you can use this pattern:

/^\d+(?: *- *[a-zA-Z0-9 ]+)+$/

examples of use:

var yourString = "1234441 - Benjamin Franklin ISD Accelerated";
var pattern = /^\d+(?: *- *[a-zA-Z0-9 ]+)+$/; 
if (pattern.test(yourString))
    ... true ...
else
    ... false ...

or with RegExp

var pattern = new RegExp("^\d+(?: *- *[a-zA-Z0-9 ]+)+$");
if (pattern.test(yourString)) ...

note that you must remove delimiters and put the possible flags as second parameter when you use the RegExp constructor (the object syntax). However pattern is a RegExp object in both cases.

explanation:

^               begining of the string
\d+             one or more digits
(?:             non capturing group
 *              a space zero or more times
-               literal -
 *              a space zero or more times
[a-zA-Z0-9 ]+   a letter, a digit or a space one or more times
)+              close non capturing group, one or more times
$               end of the string 

Upvotes: 4

Joseph Myers
Joseph Myers

Reputation: 6552

Two solutions:

  1. the first one is designed to be used using .test to return true or false.

    var orgIdCheck = /\d+\s*-\s*[-\w\s]+\w/;
    var validId = orgIdCheck.test(*possible org ID string*);
    if (validId) {} /* do something */
    else {} /* do something else */
    
  2. the second one is designed to return a two-element array containing the numeric ID and the organization name

    var orgIdComponents = /(\d+)|(\w[-\w\s]*\w)/g;
    var idParts = *validated org ID string*.match(orgIdComponents);
    /* idParts[0] will be the numeric ID, and idParts[1] the name */
    

Note that the second regular expression doesn't check to make sure that the string contains both a numerical ID and a name, so you should validate using the first regular expression before using the second one. (The second one can return weird things if you don't validate the org ID string beforehand.)

Depending on how paranoid you are about bad inputs, you can put a caret ^ and dollar sign $ before and after the orgIdCheck expression (within the / and /). But you might end up getting false for some valid inputs, just because of extra spaces.

Upvotes: 1

Related Questions