How to Change Validation Messages for WPML

Overview

Would you like to use WPML or Polylang to change validation messages on your forms? In WPForms, these messages are displayed inline and fields are validated “on the fly” using Javascript. Using a bit of PHP and Javascript these messages can be easily changed .

In some cases, like when using WPML, these messages may need to be conditionally different. With this in mind, WPML provides a constant that can be used to check the language your visitor currently is using.  That constant is ICL_LANGUAGE_CODE.

You can read about that in the WPML API documentation.

We can use that constant to check and see if the validation message should be changed.

Setup

Below is an example that changes the required field validation message depending on the language WPML is using. This method will work for the Polylang plugin as well.

Copy this code to your site to customize validation strings.

/**
 * Customize strings for WPForms form validation depending on
 * the language WPML is using.
 * 
 * @link https://wpforms.com/developers/change-validation-messages-for-wpml/
 *
 */
function wpf_dev_validation_strings() {

	if ( defined( 'ICL_LANGUAGE_CODE' ) && 'fr' == ICL_LANGUAGE_CODE ) {
		// French
		?>
		<script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery.extend(jQuery.validator.messages, {
                required: "Ce champ est requis."
            });
        });
		</script>
		<?php
	} elseif ( defined( 'ICL_LANGUAGE_CODE' ) && 'es' == ICL_LANGUAGE_CODE )  {
		// Spanish
		?>
		<script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery.extend(jQuery.validator.messages, {
                required: "Este campo es requerido."
            });
        });    
		</script>
		<?php
	}
	// Otherwise use default English messages
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_validation_strings', 20 );

And that’s it! You’ve now successfully changed the validation messages. Would you like to change the date picker strings to match the language as well? Take a look at our article on How to Modify Date Field Date Picker Strings.

FAQ

Q: What about the other validation messages?

A: This is a complete list of all the validation messages that you can alter using the JavaScript code mentioned above.

/**
 * Customize strings for WPForms form validation depending on
 * the language WPML is using.
 * 
 * @link https://wpforms.com/developers/change-validation-messages-for-wpml/
 *
 */
function wpf_dev_validation_strings() {

	if ( defined( 'ICL_LANGUAGE_CODE' ) && 'fr' == ICL_LANGUAGE_CODE ) {
		// French
		?>
		<script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery.extend(jQuery.validator.messages, {
                required: "Ce champ est requis.",
                url: "Please enter a valid URL.",
                email: "Veuillez entrer une URL valide.",
                email_suggestion: "Voulez-vous dire {suggestion}?",
                number: "S'il vous plait, entrez un nombre valide.",
                confirm: "Les valeurs de champ ne correspondent pas.",
                checklimit: "Vous avez dépassé le nombre de sélections autorisées: {#}.",
                extension: "Le type de fichier n'est pas autorisé.",
                maxsize: "Le fichier dépasse la taille maximale autorisée.",
                time12h: "Veuillez entrer l'heure au format 12 heures AM / PM (par exemple, 8h45).",
                time24h: "Veuillez entrer l'heure au format 24 heures (par exemple, 22h45).",
                requiredpayment: "Le paiement est requis.",
                creditcard: "Veuillez entrer un numéro de carte de crédit valide.",
                post_max_size: "La taille totale des fichiers sélectionnés {totalSize} Mo dépasse la limite autorisée {maxSize} Mo.",
                validation_unique: "La valeur doit être unique.",
            });
        });
		</script>
		<?php
	} elseif ( defined( 'ICL_LANGUAGE_CODE' ) && 'es' == ICL_LANGUAGE_CODE )  {
		// Spanish
		?>
		<script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery.extend(jQuery.validator.messages, {
                required: "Este campo es requerido.",
                url: "Por favor introduzca un URL válido.",
                email: "Por favor, introduce una dirección de correo electrónico válida.",
                email_suggestion: "¿Quiso decir {sugerencia}?",
                number: "Por favor ingrese un número valido.",
                confirm: "Los valores de campo no coinciden.",
                checklimit: "Ha excedido el número de selecciones permitidas: {#}.",
                extension: "El tipo de archivo no está permitido.",
                maxsize: "El archivo excede el tamaño máximo permitido.",
                time12h: "Ingrese la hora en formato AM / PM de 12 horas (por ejemplo, 8:45 AM).",
                time24h: "Ingrese la hora en formato de 24 horas (por ejemplo, 22:45).",
                requiredpayment: "Se requiere pago.",
                creditcard: "Por favor, introduzca un número de tarjeta de crédito válida.",
                post_max_size: "El tamaño total de los archivos seleccionados {totalSize} Mb excede el límite permitido {maxSize} Mb.",
                validation_unique: "El valor debe ser único.",
            });
        });    
		</script>
		<?php
	}
	// Otherwise use default English messages
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_validation_strings', 20 );