How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]?

后端 未结 6 2160
南笙
南笙 2020-12-05 05:24

I\'m trying to create a regex to verify that a given string only has alpha characters a-z or A-Z. The string can be up to 25 letters long. (I\'m not sure if regex can check

6条回答
  •  [愿得一人]
    2020-12-05 06:09

    The string can be up to 25 letters long. (I'm not sure if regex can check length of strings)

    Regexes ceartanly can check length of a string - as can be seen from the answers posted by others.

    However, when you are validating a user input (say, a username), I would advise doing that check separately.

    The problem is, that regex can only tell you if a string matched it or not. It won't tell why it didn't match. Was the text too long or did it contain unallowed characters - you can't tell. It's far from friendly, when a program says: "The supplied username contained invalid characters or was too long". Instead you should provide separate error messages for different situations.

提交回复
热议问题