-6

If the first name, last name and the nick name starts with the special chars like !@#$%^&*()_+?></ then show the error message like this "Enter valid first name/ last name/nick name"

2 Answers2

1

You can use String#matches() to check if a name begins with any of the special characters on your blacklist:

if (name.matches("(?:!|@|#|\\$|%|\\^|&|\\*|\\(|\\)|_|\\+|\\?|>|‌​<).*")) {
    System.out.println("Enter valid name");
} 

Another way:

String specialChars = "!@#$%^&*()_+?><";
if (specialChars.indexOf(name.charAt(0)) != -1) {
    System.out.println("Enter valid name");
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

try below code

String regex = "!@#$%^&*()_+?></ *";

if (regex.contains(firstname.charAt(0))){
   //--- set error
}
sajan
  • 389
  • 5
  • 11