Prevent international character input

I’m working on a Phoenix app which collects data from our users. Some of the places we are sending data don’t currently support utf8 inputs. Until we can fix those issues, we’d like to prevent users from being able to input non-ascii characters. What’s the easiest way for us to accomplish this?

Scan the input for any codepoint larger than 127 and give the user an error message when you find some.

This is the very least. There might be ways to do similar on the client side while the user enters his data, but you always should check on server side again.

def valid_ascii?(<<c::utf8, rest::binary>>) when c > 127, do: false
def valid_ascii?(<<_::utf8, rest::binary>>), do: valid_ascii?(rest)
def valid_ascii?(<<>>), do: true
1 Like

Thank you! Super helpful.