1

Not wanting to end up with a chameleon question I have started a new one, I am trying to use php regex to match the following...

  • Only allow alphanumeric characters
  • Have at least 2 characters
  • Does not consist of only numbers

I have this regex so far http://regex101.com/r/lL3uT4....

^(?![0-9]+$)[a-zA-Z0-9 ]{2,}$

This is working but the following data still validates when i want it to fail...

47 (note the space after 47)
   (two spaces next to each other)

I understand it is to do with spaces but I still need the following to validate...

47 International
3 apple

I think what I am trying to do is allow spaces but fail them if the string consists entirely of spaces or if there is a space but then no character afterwards

1 Answer 1

1

You can use:

^(?! )(?![0-9]+$)[a-zA-Z0-9 ]+[a-zA-Z0-9]$
Online Demo
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks once again, it's good comparing the two answers so I can see whats changed. Much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.