Hey all, so I'm trying to allow some text input which goes through a regex check before it's sent off. I want the text to only include A-Z, 0-9, and the space " " character. Here is my code now:
if(!title.matches("[a-zA-Z0-9_]+") {
//fail
}
else {
//success
}
However this still gives the //fail
result when I input something like "This is a test"
Any ideas? Thanks all.
You're not including the space in the Regex. Try the following:
if (!title.matches("[a-zA-Z0-9 ]+"))
\s
allows for any ASCII whitespace character. Consider using that rather than " ".
if(!title.matches("[a-zA-Z0-9_\s]+")) {
//fail
}
else {
//success
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With