Validation and Formatting Functions
These functions need reference to both MVCControlToolkit.Controls-x.x.x.js or the smaller MVCControlToolkit.Controls.Core-x.x.x.js and jquery-1.4.2.js. (or better)
You can call from javascript some Mvc Controls Toolkit functions that perform useful tools like formatting values, or verifying if an input filed or form is valid.
Client Formatting with MvcControlsToolkit_FormatDisplay
The MvcControlsToolkit_FormatDisplay function allow client-side formatting both in Mvc2, Mvc 3, with unobtrusive ajax or Microsoft ajax on. It takes care of all details:
function MvcControlsToolkit_FormatDisplay(value, format, dataType, prefix, postfix, nullString)
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:
var MvcControlsToolkit_DataType_String = 0;
var MvcControlsToolkit_DataType_UInt = 1;
var MvcControlsToolkit_DataType_Int = 2;
var MvcControlsToolkit_DataType_Float = 3;
var MvcControlsToolkit_DataType_DateTime = 4;
A globalized number of date string can also be parsed to get the relative javascript typewith:
function MvcControlsToolkit_Parse(value, dataType)
Validating a form or a single input field.
The MvcControlsToolkit_FormIsValid function perform form validation:
function MvcControlsToolkit_FormIsValid(elementField, validationType)
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:
var ValidationType_StandardClient = "StandardClient";
var ValidationType_UnobtrusiveClient = "UnobtrusiveClient";
var ValidationType_Server = "Server";
If the chose value is ValidationType_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:
function MvcControlsToolkit_Validate(fieldName, validationType)
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:
function Setup_Ajax_ClientValidation(formId, validationType)
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 conten 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:
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 = "Setup_Ajax_ClientValidation('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".