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"
Asked
Active
Viewed 108 times
-6
-
4What's your question? – 4castle Feb 02 '17 at 08:38
-
1use regex for validation – Vivek Mishra Feb 02 '17 at 08:38
-
1First and foremost stackoverflow isn't the place where someone will solve the entire problem for you. You need to show us the code and pinpoint the place when you encountere a problem. – mic4ael Feb 02 '17 at 08:39
-
6Wecome to StackOverflow. How to ask a good question -> http://stackoverflow.com/help/how-to-ask – adalpari Feb 02 '17 at 08:40
-
2Possible duplicate of [Java regular expression match](http://stackoverflow.com/questions/4463867/java-regular-expression-match) – Nayan Srivastava Feb 02 '17 at 08:42
-
1`if (name.matches("(?:!|@|#|\\$|%|\\^|&|\\*|\\(|\\)|_|\\+|\\?|>|<).*")) { System.out.println("Enter valid name"); }` – Tim Biegeleisen Feb 02 '17 at 08:42
-
why people downvoting this. I think he asked a nice question and if it is not a duplicate then whats the issue? – Ankush Bist Feb 02 '17 at 08:50
-
@Ankush Bist Question might be nice! But there is a way to ask! He has not shown any attempts that he made or any related thing and of course there are many smiler questions here at SO – Charuක Feb 02 '17 at 09:08
2 Answers
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