Validation and Formatting Functions

These functions need reference to MVCControlToolkit.Controls.Core-x.x.x.js.

Available Mvc Controls Toolkit javascript utilities that perform useful stuffs like formatting values, or verifying if an input fieldd or form is valid.

Client Formatting with mvcct.globalize.formatDisplay

The mvcct.globalize.formatDisplay  function allow client-side formatting. It takes care of all details:

 

mvcct.globalize.formatDisplay(value, format, dataType, prefix, postfix, nullString) 
version>=3.0.0
MvcControlsToolkit_FormatDisplay(value, format, dataType, prefix, postfix, nullString)
version<3.0.0

 

 

Where value is the value to be formatted format a format string such as 'n' or 'd', dataType the declared dataType, while prefix and posfix are added to the result of applying the format string to the provided value. nullString is used in case value is null.

Allowed dataTypes are the constants:

mvcct.globalize.stringType = 0;
mvcct.globalize.uintType = 1;
mvcct.globalize.intType = 2;
mvcct.globalize.floatType = 3;
mvcct.globalize.dateTimeType = 4;
version >= 3.0.0
MvcControlsToolkit_DataType_String = 0;
MvcControlsToolkit_DataType_UInt = 1;
MvcControlsToolkit_DataType_Int = 2;
MvcControlsToolkit_DataType_Float = 3;
MvcControlsToolkit_DataType_DateTime = 4;
version < 3.0.0

 

A globalized number or date string may also be parsed to get the relative javascript typewith: 

 

mvcct.globalize.parse(value, dataType) version >= 3.0.0
MvcControlsToolkit_Parse(value, dataType) version < 3.0.0

 

Validating a form or a single input field.

The mvcct.unobtrusive.formIsValid function performs form validation:

 

mvcct.unobtrusive.formIsValid(elementField, validationType) version >= 3.0.0

MvcControlsToolkit_FormIsValid(elementField, validationType) version < 3.0.0

 

Where the first parameter is the id of an input field contained in the form, and the second parameter is one of the constants below:

"StandardClient"; for version >= 3.0.0 old Mvc2 validation is no more supported
"UnobtrusiveClient";
"Server";

 

If the chosen value is  "Server" no validation is performed and the function always return true.

As a result of the validation all errors are shown and the function returns true in case of success or false in the case of failure.

The same job on a single input field is performed by the function:

 

mvcct.unobtrusive.validate(fieldName, validationType) version >= 3.0.0
MvcControlsToolkit_Validate(fieldName, validationType) version < 3.0.0

 

Parsing for validation new Ajax supplied content

If an ajax call refill a whole form, client validation can be enabled for the new content by calling the javascript function:

mvcct.unobtrusive.prepareAjaxFormClientValidation(formId, validationType) version >= 3.0.0
Setup_Ajax_ClientValidation(formId, validationType) version < 3.0.0

 

where validationType is one of the javascript constants defined before, and formId is the Id of the form to be re-initialized for validation.

If the new content doesn't substitute completely the content of a form but it is just added or replace a part of the form content, client validation can be enabled for the new content only if we are using unobstructive validation by using the function:

$.validator.unobtrusive.parseExt(selector)

where selector is the jQuery selector that defines the content to parse for unobtrusive validation.

The functions above can be called in the OnSuccess of the ajax call as shown below:

 

@using (Ajax.BeginForm("Index", "Home",
    new AjaxOptions
    {
        UpdateTargetId="main",
        OnSuccess = "mvcct.unobtrusive.prepareAjaxFormClientValidation('myForm', ValidationType_UnobtrusiveClient)" }, new {id="myForm"})) {

 

In case you are using old Microsof Ajax, for instance in case of Mvc 2 projects, The content need to be parsed for javascript code BEFORE being parsed for validation with the function: GlobalEvalScriptAndDestroy(element) where element is the root DOM node of the newly added content. In the above example element is the DOM node whose id is "main".

Last edited Oct 20, 2014 at 2:46 PM by frankabbruzzese, version 18

Comments

No comments yet.