clem
How to validate full string with Regex
I currently developing a rest API with elixir and plug.
I will like to validate user input to accept only letters, but when I use the regex, it only validates a letter, not the full string.
Example:
String.match?(“A”, ~r/[1]$/i) returns true, but when I try String.match?(“foo”, ~r/[2]$/i) returns false.
Any help, please?
First Post!
NobbZ
The regex asks for a single lowercase letter from the Latin alphabet. If you want to have at least one of those and do not care for an upper bound you need to use the + quantifier: ~r/^[a-z]+$/.
Most Liked
NobbZ
Letters and numbers is broad… I consider ä a letter, do you? What’s about Chinese scripts? Or kyrillic? Greek? Persian numbers? Do you consider those as valid?
If you restrict to Arabic numerals and Latin letters in upper and lowercase it’s something like this:
~r/^[a-zA-Z0-9]+$/
zachgarwood
If you don’t mind underscores in there as well, you could use the \w “word characters” class, for example:
~r/^\w+$/
The \w class is equivalent to [A-Za-z0-9_].
Last Post!
clem
Popular in Discussions
Other popular topics
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









