Home cshtml validation message text is removed via an unfamiliar jquery function
Reply: 0

cshtml validation message text is removed via an unfamiliar jquery function

ChasetopherB
1#
ChasetopherB Published in 2018-01-12 19:26:51Z

I am running an MVC 5 web application with an auto-generated Entity Framework data model. It also uses JQuery and JQuery validate. I have a text box on a CRUD screen that must contain a decimal number with 5, 2 precision and scale, respectively.

JQuery validate does not have precision and scale validation, so I have to write a custom JavaScript function for this.

The function disables the submit button when the text box format is incorrect, and displays a message via the @Html.ValidationMessageinner-html text.

It all works, except when tabbing out of the text box field, the message text clears out.

Here is the text box:

        <div class="form-group">
            @Html.LabelFor(model => model.WeeklyAccrual, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.WeeklyAccrual, new { htmlAttributes = new { @class = "form-control", @id = "accrboxCreate", type = "number", onkeyup = "validateAccrualBox(this.value)", name = "weeklyAccruals" } })
                @Html.ValidationMessageFor(model => model.WeeklyAccural, "", new { @class = "text-danger", @id = "accrValidationMessageCreate" })
            </div>
        </div>

And here is the validation function:

function validateAccrualBox(v) {
    var regex = new RegExp("^[0-9]{1,5}(?:\.[0-9]{1,2})?$");
    if (regex.test(v) || document.getElementById("accrboxCreate").value === "") {
        //allow text input
        console.log(v);
        console.log("Valid");
        document.getElementById("accrValidationMessageCreate").innerHTML = ""
        document.getElementById("submitCreate").disabled=false;
    } else {
        //don't allow text input
        //display message "too many decimal places"
        console.log(v);
        console.log("Invalid");
        document.getElementById("accrValidationMessageCreate").innerHTML = "Correct format: 99999.99"
        document.getElementById("submitCreate").disabled=true;
    }

}

I have found that the culprit is a Jquery function:

if ( !(eventHandle = elemData.handle) ) {
            eventHandle = elemData.handle = function( e ) {
                // Discard the second event of a jQuery.event.trigger() and
                // when an event is called after a page has unloaded
                return typeof jQuery !== core_strundefined && (!e || jQuery.event.triggered !== e.type) ?
                    jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
                    undefined;
            };
            // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
            eventHandle.elem = elem;
        }

Does anyone know what is happening here?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316361 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO