Restrict Text in JTextField
 09 November 2017
 Java | Swing
 Rodrigo Costa Corrêa

Often we need to limit the text that the user types in some form fields. In the email field, for example, the user can not type accents or special characters. One way to resolve this is after submitting the form, where the text is validated and an error message is sent to the user. This irritates the user a lot because sometimes he fills an immense form and at the end he receives the error message.

I've created these functions to be able to restrict the text typed in JTextField. So, if the user types an illegal character, the character will simply not appear in the JTextField and the user can know at the moment that he is typing that the typed character is not allowed in the field that he is filling.

Java class with functions:

import java.awt.event.KeyEvent;
import java.text.Normalizer;

public class StringFunctions {

	public static void onlyText(KeyEvent onlyText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ áíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ"
				.contains(onlyText.getKeyChar() + ""))
			onlyText.consume();
	}

	public static void onlyTextAndNumber(KeyEvent onlyTextAndNumber) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ áíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ0987654321"
				.contains(onlyTextAndNumber.getKeyChar() + ""))
			onlyTextAndNumber.consume();
	}

	public static void onlySimpleText(KeyEvent onlySimpleText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ".contains(onlySimpleText.getKeyChar() + ""))
			onlySimpleText.consume();
	}

	public static void onlyLoginText(KeyEvent onlyLoginText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(onlyLoginText.getKeyChar() + ""))
			onlyLoginText.consume();
	}

	public static void onlyPasswordText(KeyEvent onlyPasswordText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321!@#$%&"
				.contains(onlyPasswordText.getKeyChar() + ""))
			onlyPasswordText.consume();
	}

	public static void onlySimpleTextAndNumber(KeyEvent onlySimpleTextAndNumber) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0987654321"
				.contains(onlySimpleTextAndNumber.getKeyChar() + ""))
			onlySimpleTextAndNumber.consume();
	}

	public static void onlyNumber(KeyEvent onlyNumber) {
		if (!"0987654321".contains(onlyNumber.getKeyChar() + ""))
			onlyNumber.consume();
	}

	public static void onlyTextAndNumberAndSpecial(KeyEvent onlyTextAndNumberAndSpecial) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ 0987654321_/$%,.|:@()-ºª"
				.contains(onlyTextAndNumberAndSpecial.getKeyChar() + ""))
			onlyTextAndNumberAndSpecial.consume();
	}

	public static void onlyAddressText(KeyEvent onlyAddressText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ 0987654321-,.()"
				.contains(onlyAddressText.getKeyChar() + ""))
			onlyAddressText.consume();
	}

	public static void onlyEmailText(KeyEvent onlyEmailText) {
		if (!"abcdefghijklmnopqrstuvwxyz0987654321_@-.".contains(onlyEmailText.getKeyChar() + ""))
			onlyEmailText.consume();
	}

	public static void onlyDateTimeText(KeyEvent onlyDateTimeText) {
		if (!"0987654321-:/ ".contains(onlyDateTimeText.getKeyChar() + ""))
			onlyDateTimeText.consume();
	}

	public static void onlyWebsiteText(KeyEvent onlyWebsiteText) {
		if (!"abcdefghijklmnopqrstuvwxyz0987654321:/-.".contains(onlyWebsiteText.getKeyChar() + ""))
			onlyWebsiteText.consume();
	}

	public static void onlyUpperText(KeyEvent onlyUpperText) {
		if (!"ABCDEFGHIJKLMNOPQRSTUVWXYZÁÍÚÉÂÊÔÃÕÀÜÇ ".contains(onlyUpperText.getKeyChar() + ""))
			onlyUpperText.consume();
	}

	public static void onlyLowerText(KeyEvent onlyLowerText) {
		if (!"abcdefghijklmnopqrstuvwxyzáíúéâêôãõàüç ".contains(onlyLowerText.getKeyChar() + ""))
			onlyLowerText.consume();
	}

	public static void onlyLinkText(KeyEvent onlyLinkText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ0987654321-.:/?@%$&*()+={}[]<>|"
				.contains(onlyLinkText.getKeyChar() + ""))
			onlyLinkText.consume();
	}

	public static void onlyFileNameText(KeyEvent onlyFileNameText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0987654321"
				.contains(onlyFileNameText.getKeyChar() + ""))
			onlyFileNameText.consume();
	}

	public static void onlyAllText(KeyEvent onlyAllText) {
		if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáíúéóâêôãõàüçÁÍÚÉÓÂÊÔÃÕÀÜÇ 0987654321-.:/?@%$&*()+={}[]<>|'"
				.contains(onlyAllText.getKeyChar() + ""))
			onlyAllText.consume();
	}
	
	public static String removeAccents(String str) {
	    return Normalizer.normalize(str, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
	}
}

Example of Use:

JTextField tfEmail = new JTextField();
		tfEmail.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent ke) {
				StringFunctions.onlyEmailText(ke);
			}
		});