3

Here is the regex I currently have (which kind of works):

$regex = '/[\w ]{7,30}/';

My revision looks like what I want, but it does not work at all:

$regex = '^[\w ]{7,30}$';

Here is how I am using the regex:

public function isValid( $value )
{

    $regex = '/^[\w ]{7,30}$/';
    return preg_match( $regex, $value ) ? true : false;

}

I am trying to match the following:

  • Any lower/upper case letter
  • Any digit
  • Can contain spaces
  • Cannot contain line breaks or tab space
  • Minimum of 7 characters
  • Maximum of 30 characters

Valid inputs:

  • Testing
  • Test ing
  • Test123
  • Test 123
  • Test___

Invalid inputs:

  • Testing#
  • Testin8+
  • Tester1&

The first regex will match all valid inputs, as well as invalid (as long as the first four characters are valid, it doesn't care about the rest). The second regex matches nothing.

Nahydrin
  • 13,197
  • 12
  • 59
  • 101
  • `^…$` matches the whole string. `\w` matches chars, `\d` matches digits – knittl Feb 22 '12 at 19:24
  • also to match spaces better use \s /[\w\s]{7,30}/ – Aurimas Ličkus Feb 22 '12 at 19:25
  • @alickus `\s` includes line-breaks and tabs, so no, it's not better to use `\s`. – Nahydrin Feb 22 '12 at 19:26
  • This should give you an error because the parenthesis/delimiters is/are missing. Try `$regex = '/^[\w ]{7,30}$/';` – hakre Feb 22 '12 at 19:26
  • @Brian if yours such requirements then yes, just trying quess 'kind of works' – Aurimas Ličkus Feb 22 '12 at 19:28
  • I'm not sure I understand. The second regexp should work too, but, of course, only if you use delimiters. You haven't added delimiters, and that's the actual problem? Or did you add delimiters but forgot to include them in the question, and the regexp (with delimiters as any regexp needs) doesn't match? – rid Feb 22 '12 at 19:31
  • @Radu I added delimiters and the second regexp works just like the first, see the inputs and comment I added to the post. – Nahydrin Feb 22 '12 at 19:31
  • `preg_match('/^[\w ]{7,30}$/', 'Test 123')` works just fine for me. Can you also post the code you used? – rid Feb 22 '12 at 19:33
  • @Radu I have added the code being used, as well as invalid inputs that are passing, when they should not. – Nahydrin Feb 22 '12 at 19:36
  • `isVaild('Test 123')` returns `true` and `isValid('Testing#')` returns `false`. Is that not the expected behavior? – rid Feb 22 '12 at 19:38
  • For some reason `isValid('Testing#');` is returning true. I should point out that I am sending the values through a jQuery ajax request. – Nahydrin Feb 22 '12 at 19:43

4 Answers4

3

Try combining both like so:

$regex = '/^[\w ]{7,30}$/';
Drew Galbraith
  • 2,216
  • 4
  • 19
  • 22
  • I needed to add the delimiters as well as escape the input from my ajax request. Some special characters are not sent unless escaped. – Nahydrin Feb 22 '12 at 19:47
1

Don't forget your delimiters:

/^[\w ]{7,30}$/

SenorAmor
  • 3,351
  • 16
  • 27
1

You're missing the / at the beginning and end.

$regex = '/^[\w ]{7,30}$/';
Edwin
  • 2,074
  • 1
  • 21
  • 40
1
'/^[\w ]{7,30}$/'

You're missing the delimiters.

animuson
  • 53,861
  • 28
  • 137
  • 147
SNAG
  • 2,011
  • 2
  • 23
  • 43