Regex allowing a space character in Java

JMRboosties :

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.

Jason McCreary :

You're not including the space in the Regex. Try the following:

if (!title.matches("[a-zA-Z0-9 ]+"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java regex allowing all character expect `

Regex for allowing alphanumeric,-,_ and space

Regex for allowing alphanumeric and space

Regex for allowing character, numbers and - in javascript

java regex to replace any single character with space

Regex for character range and space

Regex for validating passwith allowing specific special character

regex replace space with tab character

Regex to match Character+space+character pattern

Regex for only allowing letters, numbers, space, commas, periods?

Gave regex for allowing only alphabets and a space but its not validating for numpad

Regex in asp.net for allowing only characters with single space?

Regex Pattern for not allowing slash on beginning and ending and not allowing special characters except slash, underscore, space

Java Regex for , not followed by a space

Java: .charAt not recognizing space character

Matching + character with Java Regex

Java Regex printing ? character

Regex - special character surrounded by letters or space

How to combine Regex with removing space and # character

Regex returns empty character space and undefined?

regex: change "white space" chracter and - character to null

Make first character lowercase and space underscore with regex

regex to not detect hashtag after a character not space

Space character not being matched by regex in .htaccess

js regex - match either space with specific character

Regex with any character and maybe a space not working

Regex locate number followed by space followed by character

Java regex: newline + white space

regex for single space and/or apostrophe in java