Skip to main content

Regex - Check a text without special characters but German, French

Special characters such as square brackets ([ ]) can cause an exception "java.util.regex.PatternSyntaxException" or something like this if we don't handle them correctly.

I had met this issue. In my case, my customers want our application should allow some characters in German and French even not allow some special characters.

The solution is that we limit the allowed characters by showing the validation message on GUI. For an instance, the message looks like the following:

"This field can't contain any special characters; only letters, numbers, underscores (_), spaces and single quotes (') are allowed."

I used Regular Expression to check it. For entering Germany and French, I actually don't have this type of keyboard, so I referred these sites:

* German characters: http://german.typeit.org/
* French characters: http://french.typeit.org/

Here is my code:
package vn.nvanhuong.practice;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpecialCharactersUtil {
 private SpecialCharactersUtil(){}
 
 public static boolean isExistNotAllowedCharacters(String source){
  Pattern regex = Pattern.compile("^[a-zA-Z_0-9_äöüÄÖÜùûüÿàâæéèêëïîôœÙÛÜŸÀÂÆÉÈÊËÏÎÔŒ' ]*$");
  Matcher matcher = regex.matcher(source);
  return !matcher.matches();
 }
}

And, some tests:
package vn.nvanhuong.practice.test;

import static org.junit.Assert.*;

import org.junit.Test;

import vn.nvanhuong.practice.SpecialCharactersUtil;

public class SpecialCharactersUtilTest {

 @Test
 public void shoulDetectSpecialCharacters() {
  String source = "~`!@#$%^&*()-+={}\\[\\]\\|:;\"<>,./?";
  assertTrue(SpecialCharactersUtil.isExistNotAllowedCharacters(source));
 }
 
 @Test
 public void shoulAllowGermanCharacters() {
  String source = "äöüÄÖÜ";
  assertFalse(SpecialCharactersUtil.isExistNotAllowedCharacters(source));
 }
 
 @Test
 public void shoulAllowFrenchCharacters() {
  String source = "ùûüÿàâæéèêëïîôœÙÛÜŸÀÂÆÉÈÊËÏÎÔŒ";
  assertFalse(SpecialCharactersUtil.isExistNotAllowedCharacters(source));
 }

 @Test
 public void shoulAllowAlphanumericAndSpacesAndUnderscoreAndSingleQuote() {
  String source = "character' special_1";
  assertFalse(SpecialCharactersUtil.isExistNotAllowedCharacters(source));
 }
}
Now, we can use the method for our validation on GUI.

Comments

Popular posts from this blog

[Snippet] CSS - Child element overlap parent

I searched from somewhere and found that a lot of people says a basic concept for implementing this feature looks like below: HTML code: <div id="parent">  <div id="child">  </div> </div> And, CSS: #parent{   position: relative;   overflow:hidden; } #child{   position: absolute;   top: -1;   right: -1px; } However, I had a lot of grand-parents in my case and the above code didn't work. Therefore, I needed an alternative. I presumed that my app uses Boostrap and AngularJs, maybe some CSS from them affects mine. I didn't know exactly the problem, but I believed when all CSS is loaded into my browser, I could completely handle it. www.tom-collinson.com I tried to create an example to investigated this problem by Fiddle . Accidentally, I just changed: position: parent; to position: static; for one of parents -> the problem is solved. Look at my code: <div class="modal-body dn-placeholder-parent-positi...

Automating deployment and managing apps on OpenShift

Previously, we maintained OpenShift templates for deploying apps in development environments as well as delivering these templates to our customers for their on-prem deployment. Customers who refer to our templates (as well as documentation) have their own configuration management tools to automate the deployment such as ArgoCD and FluxCD. My son's buildings Our developers usually modify templates (YAML) directly on OpenShift for testing and then adjust the corresponding templates stored in the Git repository in Bitbucket. This sometimes causes an issue that delivered templates are incorrect because: - Developers forget to update the templates in Git repositories. - Developers don’t test the templates Therefore, our goal was to integrate a tool into our CI/CD that can automate and manage the configuration of OpenShift apps. The delivered templates should be the ones that are able to run on our OpenShift with the following purposes: - Automate deployment from templated in Git repos...

Styling Sort Icons Using Font Awesome for Primefaces' Data Table

So far, Primefaces has used image sprites for displaying the sort icons. This leads to a problem if we want to make a different style for these icons; for example, I would make the icon "arrow up" more blurry at the first time the table loading because I want to highlight the icon "arrow down". I found a way that I can replace these icons with Font Awesome icons. We will use "CSS Pseudo-classes" to achieve it. The hardest thing here is that we should handle displaying icons in different cases. There is a case both "arrow up" and "arrow down" showing and other case is only one of these icons is shown. .ui-sortable-column-icon.ui-icon.ui-icon-carat-2-n-s { background-image: none; margin-left: 5px; font-size: 1.1666em; position: relative; } .ui-sortable-column-icon.ui-icon.ui-icon-carat-2-n-s:not(.ui-icon-triangle-1-s)::before { content: "\f106"; font-family: "FontAwesome"; position: ...

The power of acceptance test

User Story is the place PO gives his ideas about features so that developers are able to know what requirements are. Acceptance tests are these show the most valuable things of the features represented by some specific cases. Usually PO defines them, but not always. Therefore, refining existing acceptance tests – even defining new ones that cover all features of the User Story must be a worth task. Acceptance test with Given When Then pattern If we understand what we are going to do, we can complete it by 50% I have worked with some members those just start implementing the features one by one and from top to down of the User Story description. Be honest, I am the one used to be. What a risky approach! Because it might meet a case that is very easy to miss requirements or needs to re-work after finding any misunderstood things. I have also worked with some members those accept spending a long time to clarify the User Story. Reading carefully of whole User Story by defining...

A User Guide To Working With Huong

  Introduction I write this user guide to help us (you and me) have a good collaboration at work. I hope you also share yours. How I view success We all feel passionate and happy at work. We all enjoy discussing transparently. We take it easy to give and receive feedback. After all, we together develop and bring valuable applications to users. How I communicate I mostly prefer a face-to-face conversation. Just leave me a message on Slack if you don't want to come to my desk. For a big topic which takes more than 30 minutes, we should have a meeting. Only send me emails only if stuff is very formal or out-of-office hours Things I do that may annoy you I do practice the Pomodoro technique so that sometimes you see me in the "do not disturb" mode. Often to make things clear, I am at ease talking   frankly   with you. What gains and loses my trust It is easy to gain my trust when you commit to what you say. You show your passion and endeavors to achieve that. It is easy to lo...