Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HeuristicLabWeb.PluginHost/HLWebPluginHost/Scripts/MicrosoftMvcValidation.debug.js @ 5728

Last change on this file since 5728 was 4604, checked in by dkahn, 14 years ago

#1198 Imported new Plugin Host solution for the new MVC2 based web application

File size: 36.9 KB
Line 
1//!----------------------------------------------------------
2//! Copyright (C) Microsoft Corporation. All rights reserved.
3//!----------------------------------------------------------
4//! MicrosoftMvcValidation.js
5
6
7Type.registerNamespace('Sys.Mvc');
8
9////////////////////////////////////////////////////////////////////////////////
10// Sys.Mvc.Validation
11
12Sys.Mvc.$create_Validation = function Sys_Mvc_Validation() { return {}; }
13
14
15////////////////////////////////////////////////////////////////////////////////
16// Sys.Mvc.JsonValidationField
17
18Sys.Mvc.$create_JsonValidationField = function Sys_Mvc_JsonValidationField() { return {}; }
19
20
21////////////////////////////////////////////////////////////////////////////////
22// Sys.Mvc.JsonValidationOptions
23
24Sys.Mvc.$create_JsonValidationOptions = function Sys_Mvc_JsonValidationOptions() { return {}; }
25
26
27////////////////////////////////////////////////////////////////////////////////
28// Sys.Mvc.JsonValidationRule
29
30Sys.Mvc.$create_JsonValidationRule = function Sys_Mvc_JsonValidationRule() { return {}; }
31
32
33////////////////////////////////////////////////////////////////////////////////
34// Sys.Mvc.ValidationContext
35
36Sys.Mvc.$create_ValidationContext = function Sys_Mvc_ValidationContext() { return {}; }
37
38
39////////////////////////////////////////////////////////////////////////////////
40// Sys.Mvc.NumberValidator
41
42Sys.Mvc.NumberValidator = function Sys_Mvc_NumberValidator() {
43}
44Sys.Mvc.NumberValidator.create = function Sys_Mvc_NumberValidator$create(rule) {
45    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
46    /// </param>
47    /// <returns type="Sys.Mvc.Validator"></returns>
48    return Function.createDelegate(new Sys.Mvc.NumberValidator(), new Sys.Mvc.NumberValidator().validate);
49}
50Sys.Mvc.NumberValidator.prototype = {
51   
52    validate: function Sys_Mvc_NumberValidator$validate(value, context) {
53        /// <param name="value" type="String">
54        /// </param>
55        /// <param name="context" type="Sys.Mvc.ValidationContext">
56        /// </param>
57        /// <returns type="Object"></returns>
58        if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
59            return true;
60        }
61        var n = Number.parseLocale(value);
62        return (!isNaN(n));
63    }
64}
65
66
67////////////////////////////////////////////////////////////////////////////////
68// Sys.Mvc.FormContext
69
70Sys.Mvc.FormContext = function Sys_Mvc_FormContext(formElement, validationSummaryElement) {
71    /// <param name="formElement" type="Object" domElement="true">
72    /// </param>
73    /// <param name="validationSummaryElement" type="Object" domElement="true">
74    /// </param>
75    /// <field name="_validationSummaryErrorCss" type="String" static="true">
76    /// </field>
77    /// <field name="_validationSummaryValidCss" type="String" static="true">
78    /// </field>
79    /// <field name="_formValidationTag" type="String" static="true">
80    /// </field>
81    /// <field name="_onClickHandler" type="Sys.UI.DomEventHandler">
82    /// </field>
83    /// <field name="_onSubmitHandler" type="Sys.UI.DomEventHandler">
84    /// </field>
85    /// <field name="_errors" type="Array">
86    /// </field>
87    /// <field name="_submitButtonClicked" type="Object" domElement="true">
88    /// </field>
89    /// <field name="_validationSummaryElement" type="Object" domElement="true">
90    /// </field>
91    /// <field name="_validationSummaryULElement" type="Object" domElement="true">
92    /// </field>
93    /// <field name="fields" type="Array" elementType="FieldContext">
94    /// </field>
95    /// <field name="_formElement" type="Object" domElement="true">
96    /// </field>
97    /// <field name="replaceValidationSummary" type="Boolean">
98    /// </field>
99    this._errors = [];
100    this.fields = new Array(0);
101    this._formElement = formElement;
102    this._validationSummaryElement = validationSummaryElement;
103    formElement[Sys.Mvc.FormContext._formValidationTag] = this;
104    if (validationSummaryElement) {
105        var ulElements = validationSummaryElement.getElementsByTagName('ul');
106        if (ulElements.length > 0) {
107            this._validationSummaryULElement = ulElements[0];
108        }
109    }
110    this._onClickHandler = Function.createDelegate(this, this._form_OnClick);
111    this._onSubmitHandler = Function.createDelegate(this, this._form_OnSubmit);
112}
113Sys.Mvc.FormContext._Application_Load = function Sys_Mvc_FormContext$_Application_Load() {
114    var allFormOptions = window.mvcClientValidationMetadata;
115    if (allFormOptions) {
116        while (allFormOptions.length > 0) {
117            var thisFormOptions = allFormOptions.pop();
118            Sys.Mvc.FormContext._parseJsonOptions(thisFormOptions);
119        }
120    }
121}
122Sys.Mvc.FormContext._getFormElementsWithName = function Sys_Mvc_FormContext$_getFormElementsWithName(formElement, name) {
123    /// <param name="formElement" type="Object" domElement="true">
124    /// </param>
125    /// <param name="name" type="String">
126    /// </param>
127    /// <returns type="Array" elementType="Object" elementDomElement="true"></returns>
128    var allElementsWithNameInForm = [];
129    var allElementsWithName = document.getElementsByName(name);
130    for (var i = 0; i < allElementsWithName.length; i++) {
131        var thisElement = allElementsWithName[i];
132        if (Sys.Mvc.FormContext._isElementInHierarchy(formElement, thisElement)) {
133            Array.add(allElementsWithNameInForm, thisElement);
134        }
135    }
136    return allElementsWithNameInForm;
137}
138Sys.Mvc.FormContext.getValidationForForm = function Sys_Mvc_FormContext$getValidationForForm(formElement) {
139    /// <param name="formElement" type="Object" domElement="true">
140    /// </param>
141    /// <returns type="Sys.Mvc.FormContext"></returns>
142    return formElement[Sys.Mvc.FormContext._formValidationTag];
143}
144Sys.Mvc.FormContext._isElementInHierarchy = function Sys_Mvc_FormContext$_isElementInHierarchy(parent, child) {
145    /// <param name="parent" type="Object" domElement="true">
146    /// </param>
147    /// <param name="child" type="Object" domElement="true">
148    /// </param>
149    /// <returns type="Boolean"></returns>
150    while (child) {
151        if (parent === child) {
152            return true;
153        }
154        child = child.parentNode;
155    }
156    return false;
157}
158Sys.Mvc.FormContext._parseJsonOptions = function Sys_Mvc_FormContext$_parseJsonOptions(options) {
159    /// <param name="options" type="Sys.Mvc.JsonValidationOptions">
160    /// </param>
161    /// <returns type="Sys.Mvc.FormContext"></returns>
162    var formElement = $get(options.FormId);
163    var validationSummaryElement = (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(options.ValidationSummaryId)) ? $get(options.ValidationSummaryId) : null;
164    var formContext = new Sys.Mvc.FormContext(formElement, validationSummaryElement);
165    formContext.enableDynamicValidation();
166    formContext.replaceValidationSummary = options.ReplaceValidationSummary;
167    for (var i = 0; i < options.Fields.length; i++) {
168        var field = options.Fields[i];
169        var fieldElements = Sys.Mvc.FormContext._getFormElementsWithName(formElement, field.FieldName);
170        var validationMessageElement = (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(field.ValidationMessageId)) ? $get(field.ValidationMessageId) : null;
171        var fieldContext = new Sys.Mvc.FieldContext(formContext);
172        Array.addRange(fieldContext.elements, fieldElements);
173        fieldContext.validationMessageElement = validationMessageElement;
174        fieldContext.replaceValidationMessageContents = field.ReplaceValidationMessageContents;
175        for (var j = 0; j < field.ValidationRules.length; j++) {
176            var rule = field.ValidationRules[j];
177            var validator = Sys.Mvc.ValidatorRegistry.getValidator(rule);
178            if (validator) {
179                var validation = Sys.Mvc.$create_Validation();
180                validation.fieldErrorMessage = rule.ErrorMessage;
181                validation.validator = validator;
182                Array.add(fieldContext.validations, validation);
183            }
184        }
185        fieldContext.enableDynamicValidation();
186        Array.add(formContext.fields, fieldContext);
187    }
188    var registeredValidatorCallbacks = formElement.validationCallbacks;
189    if (!registeredValidatorCallbacks) {
190        registeredValidatorCallbacks = [];
191        formElement.validationCallbacks = registeredValidatorCallbacks;
192    }
193    registeredValidatorCallbacks.push(Function.createDelegate(null, function() {
194        return Sys.Mvc._validationUtil.arrayIsNullOrEmpty(formContext.validate('submit'));
195    }));
196    return formContext;
197}
198Sys.Mvc.FormContext.prototype = {
199    _onClickHandler: null,
200    _onSubmitHandler: null,
201    _submitButtonClicked: null,
202    _validationSummaryElement: null,
203    _validationSummaryULElement: null,
204    _formElement: null,
205    replaceValidationSummary: false,
206   
207    addError: function Sys_Mvc_FormContext$addError(message) {
208        /// <param name="message" type="String">
209        /// </param>
210        this.addErrors([ message ]);
211    },
212   
213    addErrors: function Sys_Mvc_FormContext$addErrors(messages) {
214        /// <param name="messages" type="Array" elementType="String">
215        /// </param>
216        if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
217            Array.addRange(this._errors, messages);
218            this._onErrorCountChanged();
219        }
220    },
221   
222    clearErrors: function Sys_Mvc_FormContext$clearErrors() {
223        Array.clear(this._errors);
224        this._onErrorCountChanged();
225    },
226   
227    _displayError: function Sys_Mvc_FormContext$_displayError() {
228        if (this._validationSummaryElement) {
229            if (this._validationSummaryULElement) {
230                Sys.Mvc._validationUtil.removeAllChildren(this._validationSummaryULElement);
231                for (var i = 0; i < this._errors.length; i++) {
232                    var liElement = document.createElement('li');
233                    Sys.Mvc._validationUtil.setInnerText(liElement, this._errors[i]);
234                    this._validationSummaryULElement.appendChild(liElement);
235                }
236            }
237            Sys.UI.DomElement.removeCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
238            Sys.UI.DomElement.addCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
239        }
240    },
241   
242    _displaySuccess: function Sys_Mvc_FormContext$_displaySuccess() {
243        var validationSummaryElement = this._validationSummaryElement;
244        if (validationSummaryElement) {
245            var validationSummaryULElement = this._validationSummaryULElement;
246            if (validationSummaryULElement) {
247                validationSummaryULElement.innerHTML = '';
248            }
249            Sys.UI.DomElement.removeCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
250            Sys.UI.DomElement.addCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
251        }
252    },
253   
254    enableDynamicValidation: function Sys_Mvc_FormContext$enableDynamicValidation() {
255        Sys.UI.DomEvent.addHandler(this._formElement, 'click', this._onClickHandler);
256        Sys.UI.DomEvent.addHandler(this._formElement, 'submit', this._onSubmitHandler);
257    },
258   
259    _findSubmitButton: function Sys_Mvc_FormContext$_findSubmitButton(element) {
260        /// <param name="element" type="Object" domElement="true">
261        /// </param>
262        /// <returns type="Object" domElement="true"></returns>
263        if (element.disabled) {
264            return null;
265        }
266        var tagName = element.tagName.toUpperCase();
267        var inputElement = element;
268        if (tagName === 'INPUT') {
269            var type = inputElement.type;
270            if (type === 'submit' || type === 'image') {
271                return inputElement;
272            }
273        }
274        else if ((tagName === 'BUTTON') && (inputElement.type === 'submit')) {
275            return inputElement;
276        }
277        return null;
278    },
279   
280    _form_OnClick: function Sys_Mvc_FormContext$_form_OnClick(e) {
281        /// <param name="e" type="Sys.UI.DomEvent">
282        /// </param>
283        this._submitButtonClicked = this._findSubmitButton(e.target);
284    },
285   
286    _form_OnSubmit: function Sys_Mvc_FormContext$_form_OnSubmit(e) {
287        /// <param name="e" type="Sys.UI.DomEvent">
288        /// </param>
289        var form = e.target;
290        var submitButton = this._submitButtonClicked;
291        if (submitButton && submitButton.disableValidation) {
292            return;
293        }
294        var errorMessages = this.validate('submit');
295        if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(errorMessages)) {
296            e.preventDefault();
297        }
298    },
299   
300    _onErrorCountChanged: function Sys_Mvc_FormContext$_onErrorCountChanged() {
301        if (!this._errors.length) {
302            this._displaySuccess();
303        }
304        else {
305            this._displayError();
306        }
307    },
308   
309    validate: function Sys_Mvc_FormContext$validate(eventName) {
310        /// <param name="eventName" type="String">
311        /// </param>
312        /// <returns type="Array" elementType="String"></returns>
313        var fields = this.fields;
314        var errors = [];
315        for (var i = 0; i < fields.length; i++) {
316            var field = fields[i];
317            var thisErrors = field.validate(eventName);
318            if (thisErrors) {
319                Array.addRange(errors, thisErrors);
320            }
321        }
322        if (this.replaceValidationSummary) {
323            this.clearErrors();
324            this.addErrors(errors);
325        }
326        return errors;
327    }
328}
329
330
331////////////////////////////////////////////////////////////////////////////////
332// Sys.Mvc.FieldContext
333
334Sys.Mvc.FieldContext = function Sys_Mvc_FieldContext(formContext) {
335    /// <param name="formContext" type="Sys.Mvc.FormContext">
336    /// </param>
337    /// <field name="_hasTextChangedTag" type="String" static="true">
338    /// </field>
339    /// <field name="_hasValidationFiredTag" type="String" static="true">
340    /// </field>
341    /// <field name="_inputElementErrorCss" type="String" static="true">
342    /// </field>
343    /// <field name="_inputElementValidCss" type="String" static="true">
344    /// </field>
345    /// <field name="_validationMessageErrorCss" type="String" static="true">
346    /// </field>
347    /// <field name="_validationMessageValidCss" type="String" static="true">
348    /// </field>
349    /// <field name="_onBlurHandler" type="Sys.UI.DomEventHandler">
350    /// </field>
351    /// <field name="_onChangeHandler" type="Sys.UI.DomEventHandler">
352    /// </field>
353    /// <field name="_onInputHandler" type="Sys.UI.DomEventHandler">
354    /// </field>
355    /// <field name="_onPropertyChangeHandler" type="Sys.UI.DomEventHandler">
356    /// </field>
357    /// <field name="_errors" type="Array">
358    /// </field>
359    /// <field name="defaultErrorMessage" type="String">
360    /// </field>
361    /// <field name="elements" type="Array" elementType="Object" elementDomElement="true">
362    /// </field>
363    /// <field name="formContext" type="Sys.Mvc.FormContext">
364    /// </field>
365    /// <field name="replaceValidationMessageContents" type="Boolean">
366    /// </field>
367    /// <field name="validationMessageElement" type="Object" domElement="true">
368    /// </field>
369    /// <field name="validations" type="Array" elementType="Validation">
370    /// </field>
371    this._errors = [];
372    this.elements = new Array(0);
373    this.validations = new Array(0);
374    this.formContext = formContext;
375    this._onBlurHandler = Function.createDelegate(this, this._element_OnBlur);
376    this._onChangeHandler = Function.createDelegate(this, this._element_OnChange);
377    this._onInputHandler = Function.createDelegate(this, this._element_OnInput);
378    this._onPropertyChangeHandler = Function.createDelegate(this, this._element_OnPropertyChange);
379}
380Sys.Mvc.FieldContext.prototype = {
381    _onBlurHandler: null,
382    _onChangeHandler: null,
383    _onInputHandler: null,
384    _onPropertyChangeHandler: null,
385    defaultErrorMessage: null,
386    formContext: null,
387    replaceValidationMessageContents: false,
388    validationMessageElement: null,
389   
390    addError: function Sys_Mvc_FieldContext$addError(message) {
391        /// <param name="message" type="String">
392        /// </param>
393        this.addErrors([ message ]);
394    },
395   
396    addErrors: function Sys_Mvc_FieldContext$addErrors(messages) {
397        /// <param name="messages" type="Array" elementType="String">
398        /// </param>
399        if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
400            Array.addRange(this._errors, messages);
401            this._onErrorCountChanged();
402        }
403    },
404   
405    clearErrors: function Sys_Mvc_FieldContext$clearErrors() {
406        Array.clear(this._errors);
407        this._onErrorCountChanged();
408    },
409   
410    _displayError: function Sys_Mvc_FieldContext$_displayError() {
411        var validationMessageElement = this.validationMessageElement;
412        if (validationMessageElement) {
413            if (this.replaceValidationMessageContents) {
414                Sys.Mvc._validationUtil.setInnerText(validationMessageElement, this._errors[0]);
415            }
416            Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
417            Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
418        }
419        var elements = this.elements;
420        for (var i = 0; i < elements.length; i++) {
421            var element = elements[i];
422            Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
423            Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
424        }
425    },
426   
427    _displaySuccess: function Sys_Mvc_FieldContext$_displaySuccess() {
428        var validationMessageElement = this.validationMessageElement;
429        if (validationMessageElement) {
430            if (this.replaceValidationMessageContents) {
431                Sys.Mvc._validationUtil.setInnerText(validationMessageElement, '');
432            }
433            Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
434            Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
435        }
436        var elements = this.elements;
437        for (var i = 0; i < elements.length; i++) {
438            var element = elements[i];
439            Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
440            Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
441        }
442    },
443   
444    _element_OnBlur: function Sys_Mvc_FieldContext$_element_OnBlur(e) {
445        /// <param name="e" type="Sys.UI.DomEvent">
446        /// </param>
447        if (e.target[Sys.Mvc.FieldContext._hasTextChangedTag] || e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
448            this.validate('blur');
449        }
450    },
451   
452    _element_OnChange: function Sys_Mvc_FieldContext$_element_OnChange(e) {
453        /// <param name="e" type="Sys.UI.DomEvent">
454        /// </param>
455        e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
456    },
457   
458    _element_OnInput: function Sys_Mvc_FieldContext$_element_OnInput(e) {
459        /// <param name="e" type="Sys.UI.DomEvent">
460        /// </param>
461        e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
462        if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
463            this.validate('input');
464        }
465    },
466   
467    _element_OnPropertyChange: function Sys_Mvc_FieldContext$_element_OnPropertyChange(e) {
468        /// <param name="e" type="Sys.UI.DomEvent">
469        /// </param>
470        if (e.rawEvent.propertyName === 'value') {
471            e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
472            if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
473                this.validate('input');
474            }
475        }
476    },
477   
478    enableDynamicValidation: function Sys_Mvc_FieldContext$enableDynamicValidation() {
479        var elements = this.elements;
480        for (var i = 0; i < elements.length; i++) {
481            var element = elements[i];
482            if (Sys.Mvc._validationUtil.elementSupportsEvent(element, 'onpropertychange')) {
483                var compatMode = document.documentMode;
484                if (compatMode && compatMode >= 8) {
485                    Sys.UI.DomEvent.addHandler(element, 'propertychange', this._onPropertyChangeHandler);
486                }
487            }
488            else {
489                Sys.UI.DomEvent.addHandler(element, 'input', this._onInputHandler);
490            }
491            Sys.UI.DomEvent.addHandler(element, 'change', this._onChangeHandler);
492            Sys.UI.DomEvent.addHandler(element, 'blur', this._onBlurHandler);
493        }
494    },
495   
496    _getErrorString: function Sys_Mvc_FieldContext$_getErrorString(validatorReturnValue, fieldErrorMessage) {
497        /// <param name="validatorReturnValue" type="Object">
498        /// </param>
499        /// <param name="fieldErrorMessage" type="String">
500        /// </param>
501        /// <returns type="String"></returns>
502        var fallbackErrorMessage = fieldErrorMessage || this.defaultErrorMessage;
503        if (Boolean.isInstanceOfType(validatorReturnValue)) {
504            return (validatorReturnValue) ? null : fallbackErrorMessage;
505        }
506        if (String.isInstanceOfType(validatorReturnValue)) {
507            return ((validatorReturnValue).length) ? validatorReturnValue : fallbackErrorMessage;
508        }
509        return null;
510    },
511   
512    _getStringValue: function Sys_Mvc_FieldContext$_getStringValue() {
513        /// <returns type="String"></returns>
514        var elements = this.elements;
515        return (elements.length > 0) ? elements[0].value : null;
516    },
517   
518    _markValidationFired: function Sys_Mvc_FieldContext$_markValidationFired() {
519        var elements = this.elements;
520        for (var i = 0; i < elements.length; i++) {
521            var element = elements[i];
522            element[Sys.Mvc.FieldContext._hasValidationFiredTag] = true;
523        }
524    },
525   
526    _onErrorCountChanged: function Sys_Mvc_FieldContext$_onErrorCountChanged() {
527        if (!this._errors.length) {
528            this._displaySuccess();
529        }
530        else {
531            this._displayError();
532        }
533    },
534   
535    validate: function Sys_Mvc_FieldContext$validate(eventName) {
536        /// <param name="eventName" type="String">
537        /// </param>
538        /// <returns type="Array" elementType="String"></returns>
539        var validations = this.validations;
540        var errors = [];
541        var value = this._getStringValue();
542        for (var i = 0; i < validations.length; i++) {
543            var validation = validations[i];
544            var context = Sys.Mvc.$create_ValidationContext();
545            context.eventName = eventName;
546            context.fieldContext = this;
547            context.validation = validation;
548            var retVal = validation.validator(value, context);
549            var errorMessage = this._getErrorString(retVal, validation.fieldErrorMessage);
550            if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(errorMessage)) {
551                Array.add(errors, errorMessage);
552            }
553        }
554        this._markValidationFired();
555        this.clearErrors();
556        this.addErrors(errors);
557        return errors;
558    }
559}
560
561
562////////////////////////////////////////////////////////////////////////////////
563// Sys.Mvc.RangeValidator
564
565Sys.Mvc.RangeValidator = function Sys_Mvc_RangeValidator(minimum, maximum) {
566    /// <param name="minimum" type="Number">
567    /// </param>
568    /// <param name="maximum" type="Number">
569    /// </param>
570    /// <field name="_minimum" type="Number">
571    /// </field>
572    /// <field name="_maximum" type="Number">
573    /// </field>
574    this._minimum = minimum;
575    this._maximum = maximum;
576}
577Sys.Mvc.RangeValidator.create = function Sys_Mvc_RangeValidator$create(rule) {
578    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
579    /// </param>
580    /// <returns type="Sys.Mvc.Validator"></returns>
581    var min = rule.ValidationParameters['minimum'];
582    var max = rule.ValidationParameters['maximum'];
583    return Function.createDelegate(new Sys.Mvc.RangeValidator(min, max), new Sys.Mvc.RangeValidator(min, max).validate);
584}
585Sys.Mvc.RangeValidator.prototype = {
586    _minimum: null,
587    _maximum: null,
588   
589    validate: function Sys_Mvc_RangeValidator$validate(value, context) {
590        /// <param name="value" type="String">
591        /// </param>
592        /// <param name="context" type="Sys.Mvc.ValidationContext">
593        /// </param>
594        /// <returns type="Object"></returns>
595        if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
596            return true;
597        }
598        var n = Number.parseLocale(value);
599        return (!isNaN(n) && this._minimum <= n && n <= this._maximum);
600    }
601}
602
603
604////////////////////////////////////////////////////////////////////////////////
605// Sys.Mvc.RegularExpressionValidator
606
607Sys.Mvc.RegularExpressionValidator = function Sys_Mvc_RegularExpressionValidator(pattern) {
608    /// <param name="pattern" type="String">
609    /// </param>
610    /// <field name="_pattern" type="String">
611    /// </field>
612    this._pattern = pattern;
613}
614Sys.Mvc.RegularExpressionValidator.create = function Sys_Mvc_RegularExpressionValidator$create(rule) {
615    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
616    /// </param>
617    /// <returns type="Sys.Mvc.Validator"></returns>
618    var pattern = rule.ValidationParameters['pattern'];
619    return Function.createDelegate(new Sys.Mvc.RegularExpressionValidator(pattern), new Sys.Mvc.RegularExpressionValidator(pattern).validate);
620}
621Sys.Mvc.RegularExpressionValidator.prototype = {
622    _pattern: null,
623   
624    validate: function Sys_Mvc_RegularExpressionValidator$validate(value, context) {
625        /// <param name="value" type="String">
626        /// </param>
627        /// <param name="context" type="Sys.Mvc.ValidationContext">
628        /// </param>
629        /// <returns type="Object"></returns>
630        if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
631            return true;
632        }
633        var regExp = new RegExp(this._pattern);
634        var matches = regExp.exec(value);
635        return (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(matches) && matches[0].length === value.length);
636    }
637}
638
639
640////////////////////////////////////////////////////////////////////////////////
641// Sys.Mvc.RequiredValidator
642
643Sys.Mvc.RequiredValidator = function Sys_Mvc_RequiredValidator() {
644}
645Sys.Mvc.RequiredValidator.create = function Sys_Mvc_RequiredValidator$create(rule) {
646    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
647    /// </param>
648    /// <returns type="Sys.Mvc.Validator"></returns>
649    return Function.createDelegate(new Sys.Mvc.RequiredValidator(), new Sys.Mvc.RequiredValidator().validate);
650}
651Sys.Mvc.RequiredValidator._isRadioInputElement = function Sys_Mvc_RequiredValidator$_isRadioInputElement(element) {
652    /// <param name="element" type="Object" domElement="true">
653    /// </param>
654    /// <returns type="Boolean"></returns>
655    if (element.tagName.toUpperCase() === 'INPUT') {
656        var inputType = (element.type).toUpperCase();
657        if (inputType === 'RADIO') {
658            return true;
659        }
660    }
661    return false;
662}
663Sys.Mvc.RequiredValidator._isSelectInputElement = function Sys_Mvc_RequiredValidator$_isSelectInputElement(element) {
664    /// <param name="element" type="Object" domElement="true">
665    /// </param>
666    /// <returns type="Boolean"></returns>
667    if (element.tagName.toUpperCase() === 'SELECT') {
668        return true;
669    }
670    return false;
671}
672Sys.Mvc.RequiredValidator._isTextualInputElement = function Sys_Mvc_RequiredValidator$_isTextualInputElement(element) {
673    /// <param name="element" type="Object" domElement="true">
674    /// </param>
675    /// <returns type="Boolean"></returns>
676    if (element.tagName.toUpperCase() === 'INPUT') {
677        var inputType = (element.type).toUpperCase();
678        switch (inputType) {
679            case 'TEXT':
680            case 'PASSWORD':
681            case 'FILE':
682                return true;
683        }
684    }
685    if (element.tagName.toUpperCase() === 'TEXTAREA') {
686        return true;
687    }
688    return false;
689}
690Sys.Mvc.RequiredValidator._validateRadioInput = function Sys_Mvc_RequiredValidator$_validateRadioInput(elements) {
691    /// <param name="elements" type="Array" elementType="Object" elementDomElement="true">
692    /// </param>
693    /// <returns type="Object"></returns>
694    for (var i = 0; i < elements.length; i++) {
695        var element = elements[i];
696        if (element.checked) {
697            return true;
698        }
699    }
700    return false;
701}
702Sys.Mvc.RequiredValidator._validateSelectInput = function Sys_Mvc_RequiredValidator$_validateSelectInput(optionElements) {
703    /// <param name="optionElements" type="DOMElementCollection">
704    /// </param>
705    /// <returns type="Object"></returns>
706    for (var i = 0; i < optionElements.length; i++) {
707        var element = optionElements[i];
708        if (element.selected) {
709            if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value)) {
710                return true;
711            }
712        }
713    }
714    return false;
715}
716Sys.Mvc.RequiredValidator._validateTextualInput = function Sys_Mvc_RequiredValidator$_validateTextualInput(element) {
717    /// <param name="element" type="Object" domElement="true">
718    /// </param>
719    /// <returns type="Object"></returns>
720    return (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value));
721}
722Sys.Mvc.RequiredValidator.prototype = {
723   
724    validate: function Sys_Mvc_RequiredValidator$validate(value, context) {
725        /// <param name="value" type="String">
726        /// </param>
727        /// <param name="context" type="Sys.Mvc.ValidationContext">
728        /// </param>
729        /// <returns type="Object"></returns>
730        var elements = context.fieldContext.elements;
731        if (!elements.length) {
732            return true;
733        }
734        var sampleElement = elements[0];
735        if (Sys.Mvc.RequiredValidator._isTextualInputElement(sampleElement)) {
736            return Sys.Mvc.RequiredValidator._validateTextualInput(sampleElement);
737        }
738        if (Sys.Mvc.RequiredValidator._isRadioInputElement(sampleElement)) {
739            return Sys.Mvc.RequiredValidator._validateRadioInput(elements);
740        }
741        if (Sys.Mvc.RequiredValidator._isSelectInputElement(sampleElement)) {
742            return Sys.Mvc.RequiredValidator._validateSelectInput((sampleElement).options);
743        }
744        return true;
745    }
746}
747
748
749////////////////////////////////////////////////////////////////////////////////
750// Sys.Mvc.StringLengthValidator
751
752Sys.Mvc.StringLengthValidator = function Sys_Mvc_StringLengthValidator(minLength, maxLength) {
753    /// <param name="minLength" type="Number" integer="true">
754    /// </param>
755    /// <param name="maxLength" type="Number" integer="true">
756    /// </param>
757    /// <field name="_maxLength" type="Number" integer="true">
758    /// </field>
759    /// <field name="_minLength" type="Number" integer="true">
760    /// </field>
761    this._minLength = minLength;
762    this._maxLength = maxLength;
763}
764Sys.Mvc.StringLengthValidator.create = function Sys_Mvc_StringLengthValidator$create(rule) {
765    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
766    /// </param>
767    /// <returns type="Sys.Mvc.Validator"></returns>
768    var minLength = rule.ValidationParameters['minimumLength'];
769    var maxLength = rule.ValidationParameters['maximumLength'];
770    return Function.createDelegate(new Sys.Mvc.StringLengthValidator(minLength, maxLength), new Sys.Mvc.StringLengthValidator(minLength, maxLength).validate);
771}
772Sys.Mvc.StringLengthValidator.prototype = {
773    _maxLength: 0,
774    _minLength: 0,
775   
776    validate: function Sys_Mvc_StringLengthValidator$validate(value, context) {
777        /// <param name="value" type="String">
778        /// </param>
779        /// <param name="context" type="Sys.Mvc.ValidationContext">
780        /// </param>
781        /// <returns type="Object"></returns>
782        if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
783            return true;
784        }
785        return (this._minLength <= value.length && value.length <= this._maxLength);
786    }
787}
788
789
790////////////////////////////////////////////////////////////////////////////////
791// Sys.Mvc._validationUtil
792
793Sys.Mvc._validationUtil = function Sys_Mvc__validationUtil() {
794}
795Sys.Mvc._validationUtil.arrayIsNullOrEmpty = function Sys_Mvc__validationUtil$arrayIsNullOrEmpty(array) {
796    /// <param name="array" type="Array" elementType="Object">
797    /// </param>
798    /// <returns type="Boolean"></returns>
799    return (!array || !array.length);
800}
801Sys.Mvc._validationUtil.stringIsNullOrEmpty = function Sys_Mvc__validationUtil$stringIsNullOrEmpty(value) {
802    /// <param name="value" type="String">
803    /// </param>
804    /// <returns type="Boolean"></returns>
805    return (!value || !value.length);
806}
807Sys.Mvc._validationUtil.elementSupportsEvent = function Sys_Mvc__validationUtil$elementSupportsEvent(element, eventAttributeName) {
808    /// <param name="element" type="Object" domElement="true">
809    /// </param>
810    /// <param name="eventAttributeName" type="String">
811    /// </param>
812    /// <returns type="Boolean"></returns>
813    return (eventAttributeName in element);
814}
815Sys.Mvc._validationUtil.removeAllChildren = function Sys_Mvc__validationUtil$removeAllChildren(element) {
816    /// <param name="element" type="Object" domElement="true">
817    /// </param>
818    while (element.firstChild) {
819        element.removeChild(element.firstChild);
820    }
821}
822Sys.Mvc._validationUtil.setInnerText = function Sys_Mvc__validationUtil$setInnerText(element, innerText) {
823    /// <param name="element" type="Object" domElement="true">
824    /// </param>
825    /// <param name="innerText" type="String">
826    /// </param>
827    var textNode = document.createTextNode(innerText);
828    Sys.Mvc._validationUtil.removeAllChildren(element);
829    element.appendChild(textNode);
830}
831
832
833////////////////////////////////////////////////////////////////////////////////
834// Sys.Mvc.ValidatorRegistry
835
836Sys.Mvc.ValidatorRegistry = function Sys_Mvc_ValidatorRegistry() {
837    /// <field name="validators" type="Object" static="true">
838    /// </field>
839}
840Sys.Mvc.ValidatorRegistry.getValidator = function Sys_Mvc_ValidatorRegistry$getValidator(rule) {
841    /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
842    /// </param>
843    /// <returns type="Sys.Mvc.Validator"></returns>
844    var creator = Sys.Mvc.ValidatorRegistry.validators[rule.ValidationType];
845    return (creator) ? creator(rule) : null;
846}
847Sys.Mvc.ValidatorRegistry._getDefaultValidators = function Sys_Mvc_ValidatorRegistry$_getDefaultValidators() {
848    /// <returns type="Object"></returns>
849    return { required: Function.createDelegate(null, Sys.Mvc.RequiredValidator.create), stringLength: Function.createDelegate(null, Sys.Mvc.StringLengthValidator.create), regularExpression: Function.createDelegate(null, Sys.Mvc.RegularExpressionValidator.create), range: Function.createDelegate(null, Sys.Mvc.RangeValidator.create), number: Function.createDelegate(null, Sys.Mvc.NumberValidator.create) };
850}
851
852
853Sys.Mvc.NumberValidator.registerClass('Sys.Mvc.NumberValidator');
854Sys.Mvc.FormContext.registerClass('Sys.Mvc.FormContext');
855Sys.Mvc.FieldContext.registerClass('Sys.Mvc.FieldContext');
856Sys.Mvc.RangeValidator.registerClass('Sys.Mvc.RangeValidator');
857Sys.Mvc.RegularExpressionValidator.registerClass('Sys.Mvc.RegularExpressionValidator');
858Sys.Mvc.RequiredValidator.registerClass('Sys.Mvc.RequiredValidator');
859Sys.Mvc.StringLengthValidator.registerClass('Sys.Mvc.StringLengthValidator');
860Sys.Mvc._validationUtil.registerClass('Sys.Mvc._validationUtil');
861Sys.Mvc.ValidatorRegistry.registerClass('Sys.Mvc.ValidatorRegistry');
862Sys.Mvc.FormContext._validationSummaryErrorCss = 'validation-summary-errors';
863Sys.Mvc.FormContext._validationSummaryValidCss = 'validation-summary-valid';
864Sys.Mvc.FormContext._formValidationTag = '__MVC_FormValidation';
865Sys.Mvc.FieldContext._hasTextChangedTag = '__MVC_HasTextChanged';
866Sys.Mvc.FieldContext._hasValidationFiredTag = '__MVC_HasValidationFired';
867Sys.Mvc.FieldContext._inputElementErrorCss = 'input-validation-error';
868Sys.Mvc.FieldContext._inputElementValidCss = 'input-validation-valid';
869Sys.Mvc.FieldContext._validationMessageErrorCss = 'field-validation-error';
870Sys.Mvc.FieldContext._validationMessageValidCss = 'field-validation-valid';
871Sys.Mvc.ValidatorRegistry.validators = Sys.Mvc.ValidatorRegistry._getDefaultValidators();
872
873// ---- Do not remove this footer ----
874// Generated using Script# v0.5.0.0 (http://projects.nikhilk.net)
875// -----------------------------------
876
877// register validation
878Sys.Application.add_load(function() {
879  Sys.Application.remove_load(arguments.callee);
880  Sys.Mvc.FormContext._Application_Load();
881});
Note: See TracBrowser for help on using the repository browser.