PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETMVCDataView/Scripts/MicrosoftMvcValidation.debug.js

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