Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are different sections on the page(ASP.net MVC) with same controls(ex:phone,email). I want the following validation message to be displayed differently in Validation Summary(with section name) and beside individual text boxes(w/o section name)

This is the validation message specified in the model
C#
[Required(ErrorMessage = "Chief Executive - |Name is required")] 
[Required(ErrorMessage = "Medical Director - |Name is required")] 


The code to replace the '|' symbol in jquery.validate.unobtrusive.js is
JavaScript
function onError(error, inputElement) { // 'this' is the form element
    var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
        replaceAttrValue = container.attr("data-valmsg-replace"),
        replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) !== false : null;

    container.removeClass("field-validation-valid").addClass("field-validation-error");
    error.data("unobtrusiveContainer", container);

    if (replace) {
        container.empty();
        error.removeClass("input-validation-error").appendTo(container);
    }
    else {
        error.hide();
    }

    error.text(error.text().substr(error.text().indexOf("|") + 1));
}

function onErrors(event, validator) { // 'this' is the form element
    var container = $(this).find("[data-valmsg-summary=true]"),
        list = container.find("ul");

    if (list && list.length && validator.errorList.length) {
        list.empty();
        container.addClass("validation-summary-errors").removeClass("validation-summary-valid");

        $.each(validator.errorList, function () {
            $("<li />").html(this.message.replace('|', '')).appendTo(list);
        });
    }
}

The pipe symbol is replaced with space and displayed as desired in local host. But when the code is deployed to servers, I am getting the same validation message (with section names) displayed in both validation summary and beside the text boxes also.

I am ready to provide other code also.

I also have this in web.config
XML
<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>


What I have tried:

jquery code not working when deployed on servers
Posted
Updated 6-Feb-20 6:36am
v3
Comments
MadMyche 5-Feb-20 15:28pm    
What does "not working when deployed" mean? Is it doing anything? Throwing an error? What does the browsers developer console show?
Member 10549164 5-Feb-20 15:56pm    
Its not throwing any error. But the jquery code above, to replace the '|' char to ' ' is not working. The message "Chief Executive - |Name is required" is displayed in validation summary as well as beside textbox.
ZurdoDev 6-Feb-20 8:49am    
Check the console in your browser for errors. Also check to make sure that jquery.js is loaded.
Richard Deeming 6-Feb-20 6:32am    
It looks like some of your code has been lost when you pasted it. For example, this line is totally mangled:
var container = $(this).find(".name) + "']"),

There's a selector missing on the line:
var container = $(this).find("")

And another missing on the line:
$("").html(this.message.replace('|', '')).appendTo(list);
Member 10549164 6-Feb-20 10:21am    
Hi Richard,

Please find code in jquery.validate.unobtrusive.js file.

\\\
(function ($) {
var $jQval = $.validator,
adapters,
data_validation = "unobtrusiveValidation";

function setValidationValues(options, ruleName, value) {
options.rules[ruleName] = value;
if (options.message) {
options.messages[ruleName] = options.message;
}
}

function splitAndTrim(value) {
return value.replace(/^\s+|\s+$/g, "").split(/\s*,\s*/g);
}

function escapeAttributeValue(value) {
// As mentioned on http://api.jquery.com/category/selectors/
return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
}

function getModelPrefix(fieldName) {
return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
}

function appendModelPrefix(value, prefix) {
if (value.indexOf("*.") === 0) {
value = value.replace("*.", prefix);
}
return value;
}

function onError(error, inputElement) { // 'this' is the form element
var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
replaceAttrValue = container.attr("data-valmsg-replace"),
replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) !== false : null;

container.removeClass("field-validation-valid").addClass("field-validation-error");
error.data("unobtrusiveContainer", container);

if (replace) {
container.empty();
error.removeClass("input-validation-error").appendTo(container);
}
else {
error.hide();
}

error.text(error.text().substr(error.text().indexOf("|") + 1));
}

function onErrors(event, validator) { // 'this' is the form element
var container = $(this).find("[data-valmsg-summary=true]"),
list = container.find("ul");

if (list && list.length && validator.errorList.length) {
list.empty();
container.addClass("validation-summary-errors").removeClass("validation-summary-valid");

$.each(validator.errorList, function () {
$("").html(this.message.replace('|', '')).appendTo(list);
});
}
}

function onSuccess(error) { // 'this' is the form element
var container = error.data("unobtrusiveContainer"),
replaceAttrValue = container.attr("data-valmsg-replace"),
replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) : null;

if (container) {
container.addClass("field-validation-valid").removeClass("field-validation-error");
error.removeData("unobtrusiveContainer");

if (replace) {
container.empty();
}
}
}
\\\

1 solution

As discussed in the comments, the local version of the site is using your modified jquery.validation.unobtrusive.js file, but the published version of your site is using the minified jquery.validation.unobtrusive.min.js file, which hasn't been modified.

The simplest way to update the minified file is to use Mads Kristensen's "Bundler & Minifier" extension:
Bundler & Minifier - Visual Studio Marketplace[^]

Once you have the extension installed, right-click on the jquery.validation.unobtrusive.js file in the solution explorer, and select Bundler & Minifier ⇒ Minify File.

Once you've done that, any future changes to the jquery.validation.unobtrusive.js file should automatically update the jquery.validation.unobtrusive.min.js file as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900