Versions: SharePoint 2013
Definitions: SharePoint forms
References:
Tags: #JavaScript, #JQuery, #SharePoint
Show and hide easily a custom error message in SharePoint forms (NewForm and EditForm) while keeping the same style of SharePoint error message
/**
* Attache et supprime un message d'erreur en dessous d'un champ avec un comportement semblable au natif (style, position)
* - Cette est fonction est utile pour la validation des champs/formulaires avec la fonction PreSaveAction()
* @requires jQuery
* @param {string} message - Custom error message to display, otherwise, it displays "Erreur."
* @param {number} fieldIndex - Index of the field on which it will append the error message
* @param {boolean} append - 'true' to append the error message and 'false' to remove it
*/
function toggleErrMsg(message, fieldIndex, append)
{
var slc, errMsg;
if(append)
{
errMsg = "<span id='Error_Custom' class='ms-formvalidation ms-csrformvalidation'><span role='alert'>" + (message || "Erreur.") + "<br></span></span>";
slc = "#part1 > table.ms-formtable > tbody > tr:nth-child(" + fieldIndex + ") > td.ms-formbody > span:first" ;
$(slc).append(errMsg);
}
else
{
slc = "#part1 > table.ms-formtable > tbody > tr:nth-child(" + fieldIndex + ") > td.ms-formbody > span:first > span";
$(slc).remove();
}
}
