TaskID
TaskID

Reputation: 53

Java check if string only contains english keyboard letters

I want to disallow users from using any special characters in their name. They should be able to use the whole english keyboard, so

a-z, 0-9, [], (), &, ", %, $, ^, °, #, *, +, ~, §, ., ,, -, ', =, }{

and so on. So they should be allowed to use every "normal" english character which you can type with your keyboard.

How can I check that?

Upvotes: 2

Views: 7077

Answers (4)

ajayg2808
ajayg2808

Reputation: 375

Use regex to match name with English alphabets.

Solution 1:

if(name.matches("[a-zA-Z]+")) {
  // Accept name
}
else {
  // Ask to enter again
}

Solution 2:

while(!name.matches("[a-zA-Z]+")) {
  // Ask to enter again
}
// Accept name

Upvotes: 2

user13784117
user13784117

Reputation: 1132

I find the requirement to be quite strange , in that I can't see the rationale behind accepting § but not, say, å, and I have not checked the list of characters you want to accept in any detail.

But, it seems to me that what you're asking is to accept any character whose codepoint value is less than 0x0080, with the oddball exception of § (0x00A7). So I'd code it to make that check explicitly, and not get involved with regular expressions. I assume you want to exclude control characters, even though they can be typed on an English keyboard.

Pseudocode:

 for each character ch in string
      if ch < 0x0020 || (ch >= 0x007f && ch != `§')
           then it's not allowed

Your requirements are oddly-stated though, in that you want to disallow "special characters" but allow `!@#$%6&*()_+' for example. What's your definition of "special character"?

For arbitrary definition of 'allowable characters' I'd use a bitset.

static BitSet valid = new Bitset();
static {
    valid.set('A', 'Z'+1);
    valid.set('a', 'z'+1);
    valid.set('0', '9'+1);
    valid.set('.');
    valid.set('_');
      ...etc...
}

then

for (int j=0; j<str.length(); j++)
    if (!valid.get(str.charAt(j))
        ...illegal...

Upvotes: 0

Joni
Joni

Reputation: 111409

You can use a regular expression for this.

Since you have lots of characters that have special meaning in a regular expression, I recommend putting them in a separate string and quoting them:

String specialCharacters = "-[]()&...";
Pattern allowedCharactersPattern = Pattern.compile("[A-Za-z0-9" + Pattern.quote(specialCharacters) +  "]*");

boolean containsOnlyAllowedCharacters(String str) {
    return allowedCharactersPattern.matcher(str).matches();
}

As for how to obtain the string of special characters in the first place, there is no way to list all the characters that can be typed with the user's current keyboard layout. In fact, since there are ways to type any Unicode character at all such a list would be useless anyway.

Upvotes: 1

Tayyab Razaq
Tayyab Razaq

Reputation: 378

We can do like:

String str = "My string";
System.out.println(str.matches("^[a-zA-Z][a-zA-Z\\s]+$"));//true

str = "My string1";
System.out.println(str.matches("^[a-zA-Z][a-zA-Z\\s]+$"));//false

Upvotes: 2

Related Questions