On Mon, Feb 24, 2014 at 9:33 AM, David Landry <[email protected]> wrote:

> There's a little bit of redundancy in there. Also, there's no length
> requirement (e.g. isPIN('') also returns true).
>
> Without adding length requirements, I'd do it like this:
>
>   function isPIN(str) {
>     return /^\+?(\d*)$/.test(str);
>   }
>
> With a length requirement, it might look like this (if PINs must be 4
> numbers long, {a,b} for a range from a to b length):
>
>   function isPIN(str) {
>     return /^\+?(\d{4})$/.test(str);
>   }
>

Ooops, missed this cruft, the \+? is redundant too. So,

  function isPIN(str) {
    return /^(\d{4})$/.test(str);
  }


---
David Landry

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to