0

How do I check a regular expression that sometimes has white space in the middle, and sometimes doesn't? For example, how can I validate a credit card number based on just length, actual content doesn't matter.

Currently I'm doing

    var pattern = /^\s*\d{4} \d{4} \d{4} \d{4}\s*$/;
    var result = pattern.test(CC);
 if (result) {/* do thing */

This returns true if the number looks like XXXX XXXX XXXX XXXX. It however, keeps return true if I do more XXXX's, so "XXXX XXXX XXXX XXXX XXXX" is still returning true. I also need to figure out how to make XXXXXXXXXXXXXXXX return true as well.

I also have another conundrum, this one may be simpler.

{
    if(myAge >= 0 && myAge <= 118)
    {
        HTML.innerHTML = "";
    }
    else 
        HTML.innerHTML = "Invalid age";
}

If you set the input to myAge = 0 in the input box, and then back space so there is no number, it keeps returning true, is there a way to assert that when the box is empty that it will return to false???

Any help on any of the problems would be much appreciated, thank you!

Gwinert
  • 67
  • 1
  • 7

2 Answers2

0

First Part:

Try this:

var pattern = /^\s*(?:\d{4}\s?){4}\s*$/;

Explanation:

^  : from the begining
\s*: spaces (0 or more)
(?:\d{4}\s?){4}: (?:...) means a group that will not be captured (will serve just for the repeatitions)
    \d{4}: 4 digits
    \s?  : a space (optional)
    {4}  : 4 times (4 of whatever in parentesis)
\s*: spaces (0 or more)
$  : the end

Second Part:

You have to check if myAge is a valid number (using isNaN) like this:

// if the myAge is not a number, less than 0 or greater than 118, then it's invalid
if(isNaN(myAge) || myAge < 0 || myAge > 118)
{
    HTML.innerHTML = "Invalid age";
}
else 
    HTML.innerHTML = "Valid age";
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • @Gwinert check the explanation! – ibrahim mahrir Feb 10 '17 at 00:05
  • @Gwinert as Keith said, you have to use numbers! – ibrahim mahrir Feb 10 '17 at 00:06
  • This helped for this problem, so thank you for that! If you have any ideas about the myAge one as well, that would be very appreciated! Thank you – Gwinert Feb 10 '17 at 00:08
  • @Gwinert I updated my answer: instead of capturing the groups, I used a non-capturing grouping (to optimize the regex)! – ibrahim mahrir Feb 10 '17 at 00:11
  • @Gwinert Check **Second part:** section in my answer! check the update on the regex as well! – ibrahim mahrir Feb 10 '17 at 00:15
  • Just a few more questions if I may, isNaN() isn't being recognized by my compiler, do I need to include anything? Also, how would I check a pattern for white space as well as a '$' Symbol? currently I'm at "/^(\s*|$)$/;" Let me know, Thank you so much! – Gwinert Feb 10 '17 at 00:28
  • @Gwinert If you want the litteral `$` sign, then you have to escape it like this: `\$` so it wont be confused with the end of string `$` (check [**this**](http://stackoverflow.com/q/3446170/6647153) to see why). – ibrahim mahrir Feb 10 '17 at 01:31
  • @Gwinert and for the `isNaN` question, What browser are you executing this code on? As far as I know `isNaN` should be present on all modern browsers. Here is the docs for it on [**MDN**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN). – ibrahim mahrir Feb 10 '17 at 01:36
0

If I got you corectly you want this /\b\d{4}\s*\d{4}\s*\d{4}\s*\d{4}\b/g

Check if it is Link

Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32