-3

I'm trying to validate user input to return valid if and only if a input string is between 1-10 chars in length and contains no whitespace or non a-z chars.

I'm using this regular expression

var re = /(\S[a-z]){1,10}/;

but this returns true when numbers are input. And if white space if input e.g

1 returns valid.
a a returns valid.

I want to restrict the input to only between l-10(amount) of letters(lowercase) What I have so far;

http://codepen.io/anon/pen/ILKkz

1 Answers1

1

Use the following expression:

var re = /^[a-z]{1,10}$/;

This will anchor your match to the beginning (^) and end ($) of your string. Also, if you just want to allow lowercase letters, all you need is the [a-z] and nothing like \S. However, if you want to allow anything but whitespace you would use ^\S{1,10}$.

Sam
  • 20,096
  • 2
  • 45
  • 71