/ext-4.0.7/src/form/field/VTypes.js

https://bitbucket.org/srogerf/javascript · JavaScript · 179 lines · 29 code · 6 blank · 144 comment · 0 complexity · 962c5773e4f5242dd7fd98d610c012ab MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @singleton
  11. * @alternateClassName Ext.form.VTypes
  12. *
  13. * This is a singleton object which contains a set of commonly used field validation functions
  14. * and provides a mechanism for creating reusable custom field validations.
  15. * The following field validation functions are provided out of the box:
  16. *
  17. * - {@link #alpha}
  18. * - {@link #alphanum}
  19. * - {@link #email}
  20. * - {@link #url}
  21. *
  22. * VTypes can be applied to a {@link Ext.form.field.Text Text Field} using the `{@link Ext.form.field.Text#vtype vtype}` configuration:
  23. *
  24. * Ext.create('Ext.form.field.Text', {
  25. * fieldLabel: 'Email Address',
  26. * name: 'email',
  27. * vtype: 'email' // applies email validation rules to this field
  28. * });
  29. *
  30. * To create custom VTypes:
  31. *
  32. * // custom Vtype for vtype:'time'
  33. * var timeTest = /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i;
  34. * Ext.apply(Ext.form.field.VTypes, {
  35. * // vtype validation function
  36. * time: function(val, field) {
  37. * return timeTest.test(val);
  38. * },
  39. * // vtype Text property: The error text to display when the validation function returns false
  40. * timeText: 'Not a valid time. Must be in the format "12:34 PM".',
  41. * // vtype Mask property: The keystroke filter mask
  42. * timeMask: /[\d\s:amp]/i
  43. * });
  44. *
  45. * In the above example the `time` function is the validator that will run when field validation occurs,
  46. * `timeText` is the error message, and `timeMask` limits what characters can be typed into the field.
  47. * Note that the `Text` and `Mask` functions must begin with the same name as the validator function.
  48. *
  49. * Using a custom validator is the same as using one of the build-in validators - just use the name of the validator function
  50. * as the `{@link Ext.form.field.Text#vtype vtype}` configuration on a {@link Ext.form.field.Text Text Field}:
  51. *
  52. * Ext.create('Ext.form.field.Text', {
  53. * fieldLabel: 'Departure Time',
  54. * name: 'departureTime',
  55. * vtype: 'time' // applies custom time validation rules to this field
  56. * });
  57. *
  58. * Another example of a custom validator:
  59. *
  60. * // custom Vtype for vtype:'IPAddress'
  61. * Ext.apply(Ext.form.field.VTypes, {
  62. * IPAddress: function(v) {
  63. * return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);
  64. * },
  65. * IPAddressText: 'Must be a numeric IP address',
  66. * IPAddressMask: /[\d\.]/i
  67. * });
  68. *
  69. * It's important to note that using {@link Ext#apply Ext.apply()} means that the custom validator function
  70. * as well as `Text` and `Mask` fields are added as properties of the `Ext.form.field.VTypes` singleton.
  71. */
  72. Ext.define('Ext.form.field.VTypes', (function(){
  73. // closure these in so they are only created once.
  74. var alpha = /^[a-zA-Z_]+$/,
  75. alphanum = /^[a-zA-Z0-9_]+$/,
  76. email = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,
  77. url = /(((^https?)|(^ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
  78. // All these messages and functions are configurable
  79. return {
  80. singleton: true,
  81. alternateClassName: 'Ext.form.VTypes',
  82. /**
  83. * The function used to validate email addresses. Note that this is a very basic validation - complete
  84. * validation per the email RFC specifications is very complex and beyond the scope of this class, although this
  85. * function can be overridden if a more comprehensive validation scheme is desired. See the validation section
  86. * of the [Wikipedia article on email addresses][1] for additional information. This implementation is intended
  87. * to validate the following emails:
  88. *
  89. * - `barney@example.de`
  90. * - `barney.rubble@example.com`
  91. * - `barney-rubble@example.coop`
  92. * - `barney+rubble@example.com`
  93. *
  94. * [1]: http://en.wikipedia.org/wiki/E-mail_address
  95. *
  96. * @param {String} value The email address
  97. * @return {Boolean} true if the RegExp test passed, and false if not.
  98. */
  99. 'email' : function(v){
  100. return email.test(v);
  101. },
  102. /**
  103. * @property {String} emailText
  104. * The error text to display when the email validation function returns false.
  105. * Defaults to: 'This field should be an e-mail address in the format "user@example.com"'
  106. */
  107. 'emailText' : 'This field should be an e-mail address in the format "user@example.com"',
  108. /**
  109. * @property {RegExp} emailMask
  110. * The keystroke filter mask to be applied on email input. See the {@link #email} method for information about
  111. * more complex email validation. Defaults to: /[a-z0-9_\.\-@]/i
  112. */
  113. 'emailMask' : /[a-z0-9_\.\-@\+]/i,
  114. /**
  115. * The function used to validate URLs
  116. * @param {String} value The URL
  117. * @return {Boolean} true if the RegExp test passed, and false if not.
  118. */
  119. 'url' : function(v){
  120. return url.test(v);
  121. },
  122. /**
  123. * @property {String} urlText
  124. * The error text to display when the url validation function returns false.
  125. * Defaults to: 'This field should be a URL in the format "http:/'+'/www.example.com"'
  126. */
  127. 'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',
  128. /**
  129. * The function used to validate alpha values
  130. * @param {String} value The value
  131. * @return {Boolean} true if the RegExp test passed, and false if not.
  132. */
  133. 'alpha' : function(v){
  134. return alpha.test(v);
  135. },
  136. /**
  137. * @property {String} alphaText
  138. * The error text to display when the alpha validation function returns false.
  139. * Defaults to: 'This field should only contain letters and _'
  140. */
  141. 'alphaText' : 'This field should only contain letters and _',
  142. /**
  143. * @property {RegExp} alphaMask
  144. * The keystroke filter mask to be applied on alpha input. Defaults to: /[a-z_]/i
  145. */
  146. 'alphaMask' : /[a-z_]/i,
  147. /**
  148. * The function used to validate alphanumeric values
  149. * @param {String} value The value
  150. * @return {Boolean} true if the RegExp test passed, and false if not.
  151. */
  152. 'alphanum' : function(v){
  153. return alphanum.test(v);
  154. },
  155. /**
  156. * @property {String} alphanumText
  157. * The error text to display when the alphanumeric validation function returns false.
  158. * Defaults to: 'This field should only contain letters, numbers and _'
  159. */
  160. 'alphanumText' : 'This field should only contain letters, numbers and _',
  161. /**
  162. * @property {RegExp} alphanumMask
  163. * The keystroke filter mask to be applied on alphanumeric input. Defaults to: /[a-z0-9_]/i
  164. */
  165. 'alphanumMask' : /[a-z0-9_]/i
  166. };
  167. })());