Johnny Chen
Johnny Chen

Reputation: 2070

regexp accepts either all numbers or all letters

I need a regexp works like this: it only accepts strings made up of all numbers or all letters (for example: "111111111111","aaaaaaaa", not "aaaaa11111").

I tried this /(^[0-9]+$) | (^[a-z]$)/.test('111111111111'), not working though. What's the right way to do it ?

Upvotes: 0

Views: 44

Answers (1)

torvin
torvin

Reputation: 7091

First, remove the spaces, and second, your [a-z] is lacking a +. So, the working version is:

/(^[0-9]+$)|(^[a-z]+$)/

Upvotes: 1

Related Questions