I have a requirement to validate if an Account name has greek or english characters in the same Word, but not in the whole field. For example, i don't want to throw an exception if account name is "Γιώργος George". This String Contains of two words, a greek one and an English one and it is valid. However, i want to throw Exception when one word consists of Characters from both languages. For example, this Account name should be invalid: "Γιώrgos". This word has three first letters Greek and the rest ones English. I am thinking that i need to identify the whole string, then break it into pieces by identifying the "Space" character meaning it's a different word.. And then making the appropriate checks... But i don't know how to do it, especially how to identify the Space... Any ideas??? Here is what i have done until now but it is not correct since it throws exception if in the whole String there are greeks and English.
public static void validations(Account acc, boolean invokedUI) {
if(acc.ea_FirstName__c != null && acc.ea_FirstName__c != ''){
String FNameRegexEng = '[ A-Za-z- . , _ / % ( ) \\\\ &]*'; //For english characters
String FNameRegexGr = '[ Α-Ωα-ω- ίΊϊΐόΌάΆέΈύΎϋΰήΉώ . , _ / % ( ) \\\\ &]*'; //For Greek characters
Pattern MyPatternEng = Pattern.compile(FNameRegexEng);
Matcher MyMatcherEng = MyPatternEng.matcher(acc.ea_FirstName__c);
Boolean resultEng = MyMatcherEng.matches();
Pattern MyPatternGr = Pattern.compile(FNameRegexGr);
Matcher MyMatcherGr = MyPatternGr.matcher(acc.ea_FirstName__c);
Boolean resultGr = MyMatcherGr.matches();
if(acc.ea_FirstName__c != null){
if (((!(resultGr))||(resultEng)==true)&&((!(resultEng))||(resultGr)==true)) {
results.outputMessage= 'Name should contain only greek or english characters';
throw new validException(results.outputMessage);
}
}
}