MicrosoftMvcValidation.debug.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. var registeredValidatorCallbacks = formElement.validationCallbacks;
  166. if (!registeredValidatorCallbacks) {
  167. registeredValidatorCallbacks = [];
  168. formElement.validationCallbacks = registeredValidatorCallbacks;
  169. }
  170. registeredValidatorCallbacks.push(Function.createDelegate(null, function() {
  171. return Sys.Mvc._validationUtil.arrayIsNullOrEmpty(formContext.validate('submit'));
  172. }));
  173. return formContext;
  174. }
  175. Sys.Mvc.FormContext.prototype = {
  176. _onClickHandler: null,
  177. _onSubmitHandler: null,
  178. _submitButtonClicked: null,
  179. _validationSummaryElement: null,
  180. _validationSummaryULElement: null,
  181. _formElement: null,
  182. replaceValidationSummary: false,
  183. addError: function Sys_Mvc_FormContext$addError(message) {
  184. /// <param name="message" type="String">
  185. /// </param>
  186. this.addErrors([ message ]);
  187. },
  188. addErrors: function Sys_Mvc_FormContext$addErrors(messages) {
  189. /// <param name="messages" type="Array" elementType="String">
  190. /// </param>
  191. if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
  192. Array.addRange(this._errors, messages);
  193. this._onErrorCountChanged();
  194. }
  195. },
  196. clearErrors: function Sys_Mvc_FormContext$clearErrors() {
  197. Array.clear(this._errors);
  198. this._onErrorCountChanged();
  199. },
  200. _displayError: function Sys_Mvc_FormContext$_displayError() {
  201. if (this._validationSummaryElement) {
  202. if (this._validationSummaryULElement) {
  203. Sys.Mvc._validationUtil.removeAllChildren(this._validationSummaryULElement);
  204. for (var i = 0; i < this._errors.length; i++) {
  205. var liElement = document.createElement('li');
  206. Sys.Mvc._validationUtil.setInnerText(liElement, this._errors[i]);
  207. this._validationSummaryULElement.appendChild(liElement);
  208. }
  209. }
  210. Sys.UI.DomElement.removeCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
  211. Sys.UI.DomElement.addCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
  212. }
  213. },
  214. _displaySuccess: function Sys_Mvc_FormContext$_displaySuccess() {
  215. var validationSummaryElement = this._validationSummaryElement;
  216. if (validationSummaryElement) {
  217. var validationSummaryULElement = this._validationSummaryULElement;
  218. if (validationSummaryULElement) {
  219. validationSummaryULElement.innerHTML = '';
  220. }
  221. Sys.UI.DomElement.removeCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
  222. Sys.UI.DomElement.addCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
  223. }
  224. },
  225. enableDynamicValidation: function Sys_Mvc_FormContext$enableDynamicValidation() {
  226. Sys.UI.DomEvent.addHandler(this._formElement, 'click', this._onClickHandler);
  227. Sys.UI.DomEvent.addHandler(this._formElement, 'submit', this._onSubmitHandler);
  228. },
  229. _findSubmitButton: function Sys_Mvc_FormContext$_findSubmitButton(element) {
  230. /// <param name="element" type="Object" domElement="true">
  231. /// </param>
  232. /// <returns type="Object" domElement="true"></returns>
  233. if (element.disabled) {
  234. return null;
  235. }
  236. var tagName = element.tagName.toUpperCase();
  237. var inputElement = element;
  238. if (tagName === 'INPUT') {
  239. var type = inputElement.type;
  240. if (type === 'submit' || type === 'image') {
  241. return inputElement;
  242. }
  243. }
  244. else if ((tagName === 'BUTTON') && (inputElement.type === 'submit')) {
  245. return inputElement;
  246. }
  247. return null;
  248. },
  249. _form_OnClick: function Sys_Mvc_FormContext$_form_OnClick(e) {
  250. /// <param name="e" type="Sys.UI.DomEvent">
  251. /// </param>
  252. this._submitButtonClicked = this._findSubmitButton(e.target);
  253. },
  254. _form_OnSubmit: function Sys_Mvc_FormContext$_form_OnSubmit(e) {
  255. /// <param name="e" type="Sys.UI.DomEvent">
  256. /// </param>
  257. var form = e.target;
  258. var submitButton = this._submitButtonClicked;
  259. if (submitButton && submitButton.disableValidation) {
  260. return;
  261. }
  262. var errorMessages = this.validate('submit');
  263. if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(errorMessages)) {
  264. e.preventDefault();
  265. }
  266. },
  267. _onErrorCountChanged: function Sys_Mvc_FormContext$_onErrorCountChanged() {
  268. if (!this._errors.length) {
  269. this._displaySuccess();
  270. }
  271. else {
  272. this._displayError();
  273. }
  274. },
  275. validate: function Sys_Mvc_FormContext$validate(eventName) {
  276. /// <param name="eventName" type="String">
  277. /// </param>
  278. /// <returns type="Array" elementType="String"></returns>
  279. var fields = this.fields;
  280. var errors = [];
  281. for (var i = 0; i < fields.length; i++) {
  282. var field = fields[i];
  283. if (!field.elements[0].disabled) {
  284. var thisErrors = field.validate(eventName);
  285. if (thisErrors) {
  286. Array.addRange(errors, thisErrors);
  287. }
  288. }
  289. }
  290. if (this.replaceValidationSummary) {
  291. this.clearErrors();
  292. this.addErrors(errors);
  293. }
  294. return errors;
  295. }
  296. }
  297. ////////////////////////////////////////////////////////////////////////////////
  298. // Sys.Mvc.FieldContext
  299. Sys.Mvc.FieldContext = function Sys_Mvc_FieldContext(formContext) {
  300. /// <param name="formContext" type="Sys.Mvc.FormContext">
  301. /// </param>
  302. /// <field name="_hasTextChangedTag" type="String" static="true">
  303. /// </field>
  304. /// <field name="_hasValidationFiredTag" type="String" static="true">
  305. /// </field>
  306. /// <field name="_inputElementErrorCss" type="String" static="true">
  307. /// </field>
  308. /// <field name="_inputElementValidCss" type="String" static="true">
  309. /// </field>
  310. /// <field name="_validationMessageErrorCss" type="String" static="true">
  311. /// </field>
  312. /// <field name="_validationMessageValidCss" type="String" static="true">
  313. /// </field>
  314. /// <field name="_onBlurHandler" type="Sys.UI.DomEventHandler">
  315. /// </field>
  316. /// <field name="_onChangeHandler" type="Sys.UI.DomEventHandler">
  317. /// </field>
  318. /// <field name="_onInputHandler" type="Sys.UI.DomEventHandler">
  319. /// </field>
  320. /// <field name="_onPropertyChangeHandler" type="Sys.UI.DomEventHandler">
  321. /// </field>
  322. /// <field name="_errors" type="Array">
  323. /// </field>
  324. /// <field name="defaultErrorMessage" type="String">
  325. /// </field>
  326. /// <field name="elements" type="Array" elementType="Object" elementDomElement="true">
  327. /// </field>
  328. /// <field name="formContext" type="Sys.Mvc.FormContext">
  329. /// </field>
  330. /// <field name="replaceValidationMessageContents" type="Boolean">
  331. /// </field>
  332. /// <field name="validationMessageElement" type="Object" domElement="true">
  333. /// </field>
  334. /// <field name="validations" type="Array" elementType="Validation">
  335. /// </field>
  336. this._errors = [];
  337. this.elements = new Array(0);
  338. this.validations = new Array(0);
  339. this.formContext = formContext;
  340. this._onBlurHandler = Function.createDelegate(this, this._element_OnBlur);
  341. this._onChangeHandler = Function.createDelegate(this, this._element_OnChange);
  342. this._onInputHandler = Function.createDelegate(this, this._element_OnInput);
  343. this._onPropertyChangeHandler = Function.createDelegate(this, this._element_OnPropertyChange);
  344. }
  345. Sys.Mvc.FieldContext.prototype = {
  346. _onBlurHandler: null,
  347. _onChangeHandler: null,
  348. _onInputHandler: null,
  349. _onPropertyChangeHandler: null,
  350. defaultErrorMessage: null,
  351. formContext: null,
  352. replaceValidationMessageContents: false,
  353. validationMessageElement: null,
  354. addError: function Sys_Mvc_FieldContext$addError(message) {
  355. /// <param name="message" type="String">
  356. /// </param>
  357. this.addErrors([ message ]);
  358. },
  359. addErrors: function Sys_Mvc_FieldContext$addErrors(messages) {
  360. /// <param name="messages" type="Array" elementType="String">
  361. /// </param>
  362. if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
  363. Array.addRange(this._errors, messages);
  364. this._onErrorCountChanged();
  365. }
  366. },
  367. clearErrors: function Sys_Mvc_FieldContext$clearErrors() {
  368. Array.clear(this._errors);
  369. this._onErrorCountChanged();
  370. },
  371. _displayError: function Sys_Mvc_FieldContext$_displayError() {
  372. var validationMessageElement = this.validationMessageElement;
  373. if (validationMessageElement) {
  374. if (this.replaceValidationMessageContents) {
  375. Sys.Mvc._validationUtil.setInnerText(validationMessageElement, this._errors[0]);
  376. }
  377. Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
  378. Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
  379. }
  380. var elements = this.elements;
  381. for (var i = 0; i < elements.length; i++) {
  382. var element = elements[i];
  383. Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
  384. Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
  385. }
  386. },
  387. _displaySuccess: function Sys_Mvc_FieldContext$_displaySuccess() {
  388. var validationMessageElement = this.validationMessageElement;
  389. if (validationMessageElement) {
  390. if (this.replaceValidationMessageContents) {
  391. Sys.Mvc._validationUtil.setInnerText(validationMessageElement, '');
  392. }
  393. Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
  394. Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
  395. }
  396. var elements = this.elements;
  397. for (var i = 0; i < elements.length; i++) {
  398. var element = elements[i];
  399. Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
  400. Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
  401. }
  402. },
  403. _element_OnBlur: function Sys_Mvc_FieldContext$_element_OnBlur(e) {
  404. /// <param name="e" type="Sys.UI.DomEvent">
  405. /// </param>
  406. if (e.target[Sys.Mvc.FieldContext._hasTextChangedTag] || e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
  407. this.validate('blur');
  408. }
  409. },
  410. _element_OnChange: function Sys_Mvc_FieldContext$_element_OnChange(e) {
  411. /// <param name="e" type="Sys.UI.DomEvent">
  412. /// </param>
  413. e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
  414. },
  415. _element_OnInput: function Sys_Mvc_FieldContext$_element_OnInput(e) {
  416. /// <param name="e" type="Sys.UI.DomEvent">
  417. /// </param>
  418. e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
  419. if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
  420. this.validate('input');
  421. }
  422. },
  423. _element_OnPropertyChange: function Sys_Mvc_FieldContext$_element_OnPropertyChange(e) {
  424. /// <param name="e" type="Sys.UI.DomEvent">
  425. /// </param>
  426. if (e.rawEvent.propertyName === 'value') {
  427. e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
  428. if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
  429. this.validate('input');
  430. }
  431. }
  432. },
  433. enableDynamicValidation: function Sys_Mvc_FieldContext$enableDynamicValidation() {
  434. var elements = this.elements;
  435. for (var i = 0; i < elements.length; i++) {
  436. var element = elements[i];
  437. if (Sys.Mvc._validationUtil.elementSupportsEvent(element, 'onpropertychange')) {
  438. var compatMode = document.documentMode;
  439. if (compatMode && compatMode >= 8) {
  440. Sys.UI.DomEvent.addHandler(element, 'propertychange', this._onPropertyChangeHandler);
  441. }
  442. }
  443. else {
  444. Sys.UI.DomEvent.addHandler(element, 'input', this._onInputHandler);
  445. }
  446. Sys.UI.DomEvent.addHandler(element, 'change', this._onChangeHandler);
  447. Sys.UI.DomEvent.addHandler(element, 'blur', this._onBlurHandler);
  448. }
  449. },
  450. _getErrorString: function Sys_Mvc_FieldContext$_getErrorString(validatorReturnValue, fieldErrorMessage) {
  451. /// <param name="validatorReturnValue" type="Object">
  452. /// </param>
  453. /// <param name="fieldErrorMessage" type="String">
  454. /// </param>
  455. /// <returns type="String"></returns>
  456. var fallbackErrorMessage = fieldErrorMessage || this.defaultErrorMessage;
  457. if (Boolean.isInstanceOfType(validatorReturnValue)) {
  458. return (validatorReturnValue) ? null : fallbackErrorMessage;
  459. }
  460. if (String.isInstanceOfType(validatorReturnValue)) {
  461. return ((validatorReturnValue).length) ? validatorReturnValue : fallbackErrorMessage;
  462. }
  463. return null;
  464. },
  465. _getStringValue: function Sys_Mvc_FieldContext$_getStringValue() {
  466. /// <returns type="String"></returns>
  467. var elements = this.elements;
  468. return (elements.length > 0) ? elements[0].value : null;
  469. },
  470. _markValidationFired: function Sys_Mvc_FieldContext$_markValidationFired() {
  471. var elements = this.elements;
  472. for (var i = 0; i < elements.length; i++) {
  473. var element = elements[i];
  474. element[Sys.Mvc.FieldContext._hasValidationFiredTag] = true;
  475. }
  476. },
  477. _onErrorCountChanged: function Sys_Mvc_FieldContext$_onErrorCountChanged() {
  478. if (!this._errors.length) {
  479. this._displaySuccess();
  480. }
  481. else {
  482. this._displayError();
  483. }
  484. },
  485. validate: function Sys_Mvc_FieldContext$validate(eventName) {
  486. /// <param name="eventName" type="String">
  487. /// </param>
  488. /// <returns type="Array" elementType="String"></returns>
  489. var validations = this.validations;
  490. var errors = [];
  491. var value = this._getStringValue();
  492. for (var i = 0; i < validations.length; i++) {
  493. var validation = validations[i];
  494. var context = Sys.Mvc.$create_ValidationContext();
  495. context.eventName = eventName;
  496. context.fieldContext = this;
  497. context.validation = validation;
  498. var retVal = validation.validator(value, context);
  499. var errorMessage = this._getErrorString(retVal, validation.fieldErrorMessage);
  500. if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(errorMessage)) {
  501. Array.add(errors, errorMessage);
  502. }
  503. }
  504. this._markValidationFired();
  505. this.clearErrors();
  506. this.addErrors(errors);
  507. return errors;
  508. }
  509. }
  510. ////////////////////////////////////////////////////////////////////////////////
  511. // Sys.Mvc.RangeValidator
  512. Sys.Mvc.RangeValidator = function Sys_Mvc_RangeValidator(minimum, maximum) {
  513. /// <param name="minimum" type="Number">
  514. /// </param>
  515. /// <param name="maximum" type="Number">
  516. /// </param>
  517. /// <field name="_minimum" type="Number">
  518. /// </field>
  519. /// <field name="_maximum" type="Number">
  520. /// </field>
  521. this._minimum = minimum;
  522. this._maximum = maximum;
  523. }
  524. Sys.Mvc.RangeValidator.create = function Sys_Mvc_RangeValidator$create(rule) {
  525. /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
  526. /// </param>
  527. /// <returns type="Sys.Mvc.Validator"></returns>
  528. var min = rule.ValidationParameters['min'];
  529. var max = rule.ValidationParameters['max'];
  530. return Function.createDelegate(new Sys.Mvc.RangeValidator(min, max), new Sys.Mvc.RangeValidator(min, max).validate);
  531. }
  532. Sys.Mvc.RangeValidator.prototype = {
  533. _minimum: null,
  534. _maximum: null,
  535. validate: function Sys_Mvc_RangeValidator$validate(value, context) {
  536. /// <param name="value" type="String">
  537. /// </param>
  538. /// <param name="context" type="Sys.Mvc.ValidationContext">
  539. /// </param>
  540. /// <returns type="Object"></returns>
  541. if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
  542. return true;
  543. }
  544. var n = Number.parseLocale(value);
  545. return (!isNaN(n) && this._minimum <= n && n <= this._maximum);
  546. }
  547. }
  548. ////////////////////////////////////////////////////////////////////////////////
  549. // Sys.Mvc.RegularExpressionValidator
  550. Sys.Mvc.RegularExpressionValidator = function Sys_Mvc_RegularExpressionValidator(pattern) {
  551. /// <param name="pattern" type="String">
  552. /// </param>
  553. /// <field name="_pattern" type="String">
  554. /// </field>
  555. this._pattern = pattern;
  556. }
  557. Sys.Mvc.RegularExpressionValidator.create = function Sys_Mvc_RegularExpressionValidator$create(rule) {
  558. /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
  559. /// </param>
  560. /// <returns type="Sys.Mvc.Validator"></returns>
  561. var pattern = rule.ValidationParameters['pattern'];
  562. return Function.createDelegate(new Sys.Mvc.RegularExpressionValidator(pattern), new Sys.Mvc.RegularExpressionValidator(pattern).validate);
  563. }
  564. Sys.Mvc.RegularExpressionValidator.prototype = {
  565. _pattern: null,
  566. validate: function Sys_Mvc_RegularExpressionValidator$validate(value, context) {
  567. /// <param name="value" type="String">
  568. /// </param>
  569. /// <param name="context" type="Sys.Mvc.ValidationContext">
  570. /// </param>
  571. /// <returns type="Object"></returns>
  572. if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
  573. return true;
  574. }
  575. var regExp = new RegExp(this._pattern);
  576. var matches = regExp.exec(value);
  577. return (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(matches) && matches[0].length === value.length);
  578. }
  579. }
  580. ////////////////////////////////////////////////////////////////////////////////
  581. // Sys.Mvc.RequiredValidator
  582. Sys.Mvc.RequiredValidator = function Sys_Mvc_RequiredValidator() {
  583. }
  584. Sys.Mvc.RequiredValidator.create = function Sys_Mvc_RequiredValidator$create(rule) {
  585. /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
  586. /// </param>
  587. /// <returns type="Sys.Mvc.Validator"></returns>
  588. return Function.createDelegate(new Sys.Mvc.RequiredValidator(), new Sys.Mvc.RequiredValidator().validate);
  589. }
  590. Sys.Mvc.RequiredValidator._isRadioInputElement = function Sys_Mvc_RequiredValidator$_isRadioInputElement(element) {
  591. /// <param name="element" type="Object" domElement="true">
  592. /// </param>
  593. /// <returns type="Boolean"></returns>
  594. if (element.tagName.toUpperCase() === 'INPUT') {
  595. var inputType = (element.type).toUpperCase();
  596. if (inputType === 'RADIO') {
  597. return true;
  598. }
  599. }
  600. return false;
  601. }
  602. Sys.Mvc.RequiredValidator._isSelectInputElement = function Sys_Mvc_RequiredValidator$_isSelectInputElement(element) {
  603. /// <param name="element" type="Object" domElement="true">
  604. /// </param>
  605. /// <returns type="Boolean"></returns>
  606. if (element.tagName.toUpperCase() === 'SELECT') {
  607. return true;
  608. }
  609. return false;
  610. }
  611. Sys.Mvc.RequiredValidator._isTextualInputElement = function Sys_Mvc_RequiredValidator$_isTextualInputElement(element) {
  612. /// <param name="element" type="Object" domElement="true">
  613. /// </param>
  614. /// <returns type="Boolean"></returns>
  615. if (element.tagName.toUpperCase() === 'INPUT') {
  616. var inputType = (element.type).toUpperCase();
  617. switch (inputType) {
  618. case 'TEXT':
  619. case 'PASSWORD':
  620. case 'FILE':
  621. return true;
  622. }
  623. }
  624. if (element.tagName.toUpperCase() === 'TEXTAREA') {
  625. return true;
  626. }
  627. return false;
  628. }
  629. Sys.Mvc.RequiredValidator._validateRadioInput = function Sys_Mvc_RequiredValidator$_validateRadioInput(elements) {
  630. /// <param name="elements" type="Array" elementType="Object" elementDomElement="true">
  631. /// </param>
  632. /// <returns type="Object"></returns>
  633. for (var i = 0; i < elements.length; i++) {
  634. var element = elements[i];
  635. if (element.checked) {
  636. return true;
  637. }
  638. }
  639. return false;
  640. }
  641. Sys.Mvc.RequiredValidator._validateSelectInput = function Sys_Mvc_RequiredValidator$_validateSelectInput(optionElements) {
  642. /// <param name="optionElements" type="DOMElementCollection">
  643. /// </param>
  644. /// <returns type="Object"></returns>
  645. for (var i = 0; i < optionElements.length; i++) {
  646. var element = optionElements[i];
  647. if (element.selected) {
  648. if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value)) {
  649. return true;
  650. }
  651. }
  652. }
  653. return false;
  654. }
  655. Sys.Mvc.RequiredValidator._validateTextualInput = function Sys_Mvc_RequiredValidator$_validateTextualInput(element) {
  656. /// <param name="element" type="Object" domElement="true">
  657. /// </param>
  658. /// <returns type="Object"></returns>
  659. return (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value));
  660. }
  661. Sys.Mvc.RequiredValidator.prototype = {
  662. validate: function Sys_Mvc_RequiredValidator$validate(value, context) {
  663. /// <param name="value" type="String">
  664. /// </param>
  665. /// <param name="context" type="Sys.Mvc.ValidationContext">
  666. /// </param>
  667. /// <returns type="Object"></returns>
  668. var elements = context.fieldContext.elements;
  669. if (!elements.length) {
  670. return true;
  671. }
  672. var sampleElement = elements[0];
  673. if (Sys.Mvc.RequiredValidator._isTextualInputElement(sampleElement)) {
  674. return Sys.Mvc.RequiredValidator._validateTextualInput(sampleElement);
  675. }
  676. if (Sys.Mvc.RequiredValidator._isRadioInputElement(sampleElement)) {
  677. return Sys.Mvc.RequiredValidator._validateRadioInput(elements);
  678. }
  679. if (Sys.Mvc.RequiredValidator._isSelectInputElement(sampleElement)) {
  680. return Sys.Mvc.RequiredValidator._validateSelectInput((sampleElement).options);
  681. }
  682. return true;
  683. }
  684. }
  685. ////////////////////////////////////////////////////////////////////////////////
  686. // Sys.Mvc.StringLengthValidator
  687. Sys.Mvc.StringLengthValidator = function Sys_Mvc_StringLengthValidator(minLength, maxLength) {
  688. /// <param name="minLength" type="Number" integer="true">
  689. /// </param>
  690. /// <param name="maxLength" type="Number" integer="true">
  691. /// </param>
  692. /// <field name="_maxLength" type="Number" integer="true">
  693. /// </field>
  694. /// <field name="_minLength" type="Number" integer="true">
  695. /// </field>
  696. this._minLength = minLength;
  697. this._maxLength = maxLength;
  698. }
  699. Sys.Mvc.StringLengthValidator.create = function Sys_Mvc_StringLengthValidator$create(rule) {
  700. /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
  701. /// </param>
  702. /// <returns type="Sys.Mvc.Validator"></returns>
  703. var minLength = (rule.ValidationParameters['min'] || 0);
  704. var maxLength = (rule.ValidationParameters['max'] || Number.MAX_VALUE);
  705. return Function.createDelegate(new Sys.Mvc.StringLengthValidator(minLength, maxLength), new Sys.Mvc.StringLengthValidator(minLength, maxLength).validate);
  706. }
  707. Sys.Mvc.StringLengthValidator.prototype = {
  708. _maxLength: 0,
  709. _minLength: 0,
  710. validate: function Sys_Mvc_StringLengthValidator$validate(value, context) {
  711. /// <param name="value" type="String">
  712. /// </param>
  713. /// <param name="context" type="Sys.Mvc.ValidationContext">
  714. /// </param>
  715. /// <returns type="Object"></returns>
  716. if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
  717. return true;
  718. }
  719. return (this._minLength <= value.length && value.length <= this._maxLength);
  720. }
  721. }
  722. ////////////////////////////////////////////////////////////////////////////////
  723. // Sys.Mvc._validationUtil
  724. Sys.Mvc._validationUtil = function Sys_Mvc__validationUtil() {
  725. }
  726. Sys.Mvc._validationUtil.arrayIsNullOrEmpty = function Sys_Mvc__validationUtil$arrayIsNullOrEmpty(array) {
  727. /// <param name="array" type="Array" elementType="Object">
  728. /// </param>
  729. /// <returns type="Boolean"></returns>
  730. return (!array || !array.length);
  731. }
  732. Sys.Mvc._validationUtil.stringIsNullOrEmpty = function Sys_Mvc__validationUtil$stringIsNullOrEmpty(value) {
  733. /// <param name="value" type="String">
  734. /// </param>
  735. /// <returns type="Boolean"></returns>
  736. return (!value || !value.length);
  737. }
  738. Sys.Mvc._validationUtil.elementSupportsEvent = function Sys_Mvc__validationUtil$elementSupportsEvent(element, eventAttributeName) {
  739. /// <param name="element" type="Object" domElement="true">
  740. /// </param>
  741. /// <param name="eventAttributeName" type="String">
  742. /// </param>
  743. /// <returns type="Boolean"></returns>
  744. return (eventAttributeName in element);
  745. }
  746. Sys.Mvc._validationUtil.removeAllChildren = function Sys_Mvc__validationUtil$removeAllChildren(element) {
  747. /// <param name="element" type="Object" domElement="true">
  748. /// </param>
  749. while (element.firstChild) {
  750. element.removeChild(element.firstChild);
  751. }
  752. }
  753. Sys.Mvc._validationUtil.setInnerText = function Sys_Mvc__validationUtil$setInnerText(element, innerText) {
  754. /// <param name="element" type="Object" domElement="true">
  755. /// </param>
  756. /// <param name="innerText" type="String">
  757. /// </param>
  758. var textNode = document.createTextNode(innerText);
  759. Sys.Mvc._validationUtil.removeAllChildren(element);
  760. element.appendChild(textNode);
  761. }
  762. ////////////////////////////////////////////////////////////////////////////////
  763. // Sys.Mvc.ValidatorRegistry
  764. Sys.Mvc.ValidatorRegistry = function Sys_Mvc_ValidatorRegistry() {
  765. /// <field name="validators" type="Object" static="true">
  766. /// </field>
  767. }
  768. Sys.Mvc.ValidatorRegistry.getValidator = function Sys_Mvc_ValidatorRegistry$getValidator(rule) {
  769. /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
  770. /// </param>
  771. /// <returns type="Sys.Mvc.Validator"></returns>
  772. var creator = Sys.Mvc.ValidatorRegistry.validators[rule.ValidationType];
  773. return (creator) ? creator(rule) : null;
  774. }
  775. Sys.Mvc.ValidatorRegistry._getDefaultValidators = function Sys_Mvc_ValidatorRegistry$_getDefaultValidators() {
  776. /// <returns type="Object"></returns>
  777. return { required: Function.createDelegate(null, Sys.Mvc.RequiredValidator.create), length: Function.createDelegate(null, Sys.Mvc.StringLengthValidator.create), regex: Function.createDelegate(null, Sys.Mvc.RegularExpressionValidator.create), range: Function.createDelegate(null, Sys.Mvc.RangeValidator.create), number: Function.createDelegate(null, Sys.Mvc.NumberValidator.create) };
  778. }
  779. Sys.Mvc.NumberValidator.registerClass('Sys.Mvc.NumberValidator');
  780. Sys.Mvc.FormContext.registerClass('Sys.Mvc.FormContext');
  781. Sys.Mvc.FieldContext.registerClass('Sys.Mvc.FieldContext');
  782. Sys.Mvc.RangeValidator.registerClass('Sys.Mvc.RangeValidator');
  783. Sys.Mvc.RegularExpressionValidator.registerClass('Sys.Mvc.RegularExpressionValidator');
  784. Sys.Mvc.RequiredValidator.registerClass('Sys.Mvc.RequiredValidator');
  785. Sys.Mvc.StringLengthValidator.registerClass('Sys.Mvc.StringLengthValidator');
  786. Sys.Mvc._validationUtil.registerClass('Sys.Mvc._validationUtil');
  787. Sys.Mvc.ValidatorRegistry.registerClass('Sys.Mvc.ValidatorRegistry');
  788. Sys.Mvc.FormContext._validationSummaryErrorCss = 'validation-summary-errors';
  789. Sys.Mvc.FormContext._validationSummaryValidCss = 'validation-summary-valid';
  790. Sys.Mvc.FormContext._formValidationTag = '__MVC_FormValidation';
  791. Sys.Mvc.FieldContext._hasTextChangedTag = '__MVC_HasTextChanged';
  792. Sys.Mvc.FieldContext._hasValidationFiredTag = '__MVC_HasValidationFired';
  793. Sys.Mvc.FieldContext._inputElementErrorCss = 'input-validation-error';
  794. Sys.Mvc.FieldContext._inputElementValidCss = 'input-validation-valid';
  795. Sys.Mvc.FieldContext._validationMessageErrorCss = 'field-validation-error';
  796. Sys.Mvc.FieldContext._validationMessageValidCss = 'field-validation-valid';
  797. Sys.Mvc.ValidatorRegistry.validators = Sys.Mvc.ValidatorRegistry._getDefaultValidators();
  798. // ---- Do not remove this footer ----
  799. // Generated using Script# v0.5.0.0 (http://projects.nikhilk.net)
  800. // -----------------------------------
  801. // register validation
  802. Sys.Application.add_load(function() {
  803. Sys.Application.remove_load(arguments.callee);
  804. Sys.Mvc.FormContext._Application_Load();
  805. });