Norman Bird
Norman Bird

Reputation: 682

javascript regular expression test for 6 digit numbers only. comma seperated

and so this must pass:

454555, 939999 , 019999    ,727663

its for a user entering 6 digit invoice numbers. it should fail if a number is 5 or 7 digit and not 6. so 1234567, 123456 should fail, as one set is more than 6 numbers.

So far I have :

[0-9]{6}(\s*,*,\s*[0-9]{6})*

which only draw back is that it accepts 7 or more digit numbers. cant figure out if its even possible at this point to do both, test for 6 digits separated by a comma and one or more space, and all the digits have to be only 6 digits and fail if one is not.

any help appreciated. regular expressions are not my forte.

thanks

Norman

Upvotes: 3

Views: 22943

Answers (7)

Bariscode
Bariscode

Reputation: 158

You can write it using regex like the function below.

const isPassword = (password: string) => /^\d{6}$/gm.test(password);

And here is an example test file below.

  test('should recognize a valid password', () => {
    expect(isPassword('123456')).toBe(true);
    expect(isPassword('000000')).toBe(true);
  });

  test('should recognize an invalid password', () => {
    expect(isPassword('asdasda1234')).toBe(false);
    expect(isPassword('1234567')).toBe(false);
    expect(isPassword('a123456a')).toBe(false);
    expect(isPassword('11.11.11')).toBe(false);
    expect(isPassword('aaaaaa')).toBe(false);
    expect(isPassword('eeeeee')).toBe(false);
    expect(isPassword('......')).toBe(false);
    expect(isPassword('werwerwerwr')).toBe(false);
  });

Upvotes: 6

Jesko R.
Jesko R.

Reputation: 827

In order to validate the full string you can use this regex.

^(\s*\d{6}\s*)(,\s*\d{6}\s*)*,?\s*$

It works with six digits only, and you have to enter at least one 6 digit number. It also works if you have a trailing comma with whitespaces.

Upvotes: 2

user2417483
user2417483

Reputation:

There isn;t any real need for a regexp. Limit the input to only 6 characters, only accept numbers and ensure that the input has 6 digits (not show here). So you would need:

HTML

<input type='text' name='invoice' size='10' maxlength='6' value='' onkeypress='evNumersOnly(event);'>

JavaScript

<script>
function evNumbersOnly( evt ) {
  //---  only accepts numbers
  //--- this handles incompatabilities between browsers
  var theEvent = evt || window.event;
  //--- this handles incompatabilities between browsers
  var key = theEvent.keyCode || theEvent.which;
  //--- convert key number to a letter
  key = String.fromCharCode( key );
  var regex = /[0-9]/;      // Allowable characters 0-9.+-,
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    //--- this prevents the character from being displayed
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
</script>

Upvotes: 0

Justin Morgan
Justin Morgan

Reputation: 30700

I see a lot of complication here. Sounds to me like what you want is pretty simple:

/^(\d{6},)*\d{6}$/

Then we account for whitespace:

/^\s*(\d{6}\s*,\s*)*\d{6}\s*$/

But as others have noted, this is actually quite simple in JavaScript without using regex:

function check(input) {
    var parts = input.split(',');
    for (var i = 0, n = parts.length; i < n; i++) {
        if (isNaN(+parts[i].trim())) {
            return false;
        }
    }
    return true;
}

Tested in the Chrome JavaScript console.

Upvotes: 0

Paul
Paul

Reputation: 141927

Your regex does not match 7 digits in a row, but it also doesn't enforce that it matches the whole string. It just has to match some substring in the string, so it would also match each of these:

"1234512345612345612345"
"NaNaNaN  123456,    123456 BOOO!"
"!@#$%^&*({123456})*&^%$#@!"

Just add the start of string (^) and end of string ($) anchors to enforce that the whole string matches and it will work correctly:

^[0-9]{6}(\s*,*,\s*[0-9]{6})*$

Also note that ,*, could be shortened to ,+, and if you only want one comma in a row, just use ,, not ,* or ,+.

You can also replace [0-9] with \d:

^\d{6}(\s*,\s*\d{6})*$

Upvotes: 2

Phrogz
Phrogz

Reputation: 303530

Using only regex:

var commaSeparatedSixDigits = /^(?:\d{6}\s*,\s*)*\d{6}$/;
if (myInput.test(commaSeparatedSixDigits)) console.log( "Is good!" );

This says:

  • ^ - Starting at the beginning of the string
  • (?:…)* - Find zero or more of the following:
    • \d{6} - six digits
    • \s* - maybe some whitespace
    • , - a literal comma
    • \s* - maybe some whitespace
  • \d{6} - Followed by six digits
  • $ - Followed by the end of the string

Alternatively:

var commaSeparatedSixDigits = /^\s*\d{6}(?:\s*,\s*\d{6})*\s*$/;

I leave it as an exercise to you to decipher what's different about this.


Using JavaScript + regex:

function isOnlyCommaSeparatedSixDigitNumbers( str ){
  var parts = srt.split(/\s*,\s*/);
  for (var i=parts.length;i--;){
    // Ensure that each part is exactly six digit characters
    if (! /^\d{6}$/.test(parts[i])) return false;
  }
  return true;
}

Upvotes: 0

nickb
nickb

Reputation: 59709

It's accepting more than six digit numbers because you're not anchoring the text, and for some odd reason you're optionally repeating the comma. Try something like this:

^[0-9]{6}(?:\s*,\s*[0-9]{6})*$

Also note that [0-9] is equivalent to \d, so this can be rewritten more concisely as:

^\d{6}(?:\s*,\s*\d{6})*$

Upvotes: 2

Related Questions