/public/ext-4.0.0/src/form/field/VTypes.js

https://github.com/marekjs/Shacser · JavaScript · 132 lines · 29 code · 5 blank · 98 comment · 0 complexity · 05315b4c9f8e93c4088e8a6831418232 MD5 · raw file

  1. /**
  2. * @class Ext.form.field.VTypes
  3. * <p>This is a singleton object which contains a set of commonly used field validation functions.
  4. * The validations provided are basic and intended to be easily customizable and extended.</p>
  5. * <p>To add custom VTypes specify the <code>{@link Ext.form.field.Text#vtype vtype}</code> validation
  6. * test function, and optionally specify any corresponding error text to display and any keystroke
  7. * filtering mask to apply. For example:</p>
  8. * <pre><code>
  9. // custom Vtype for vtype:'time'
  10. var timeTest = /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i;
  11. Ext.apply(Ext.form.field.VTypes, {
  12. // vtype validation function
  13. time: function(val, field) {
  14. return timeTest.test(val);
  15. },
  16. // vtype Text property: The error text to display when the validation function returns false
  17. timeText: 'Not a valid time. Must be in the format "12:34 PM".',
  18. // vtype Mask property: The keystroke filter mask
  19. timeMask: /[\d\s:amp]/i
  20. });
  21. * </code></pre>
  22. * Another example:
  23. * <pre><code>
  24. // custom Vtype for vtype:'IPAddress'
  25. Ext.apply(Ext.form.field.VTypes, {
  26. IPAddress: function(v) {
  27. return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);
  28. },
  29. IPAddressText: 'Must be a numeric IP address',
  30. IPAddressMask: /[\d\.]/i
  31. });
  32. * </code></pre>
  33. * @singleton
  34. */
  35. Ext.define('Ext.form.field.VTypes', (function(){
  36. // closure these in so they are only created once.
  37. var alpha = /^[a-zA-Z_]+$/,
  38. alphanum = /^[a-zA-Z0-9_]+$/,
  39. email = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,
  40. url = /(((^https?)|(^ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
  41. // All these messages and functions are configurable
  42. return {
  43. singleton: true,
  44. alternateClassName: 'Ext.form.VTypes',
  45. /**
  46. * The function used to validate email addresses. Note that this is a very basic validation -- complete
  47. * validation per the email RFC specifications is very complex and beyond the scope of this class, although
  48. * this function can be overridden if a more comprehensive validation scheme is desired. See the validation
  49. * section of the <a href="http://en.wikipedia.org/wiki/E-mail_address">Wikipedia article on email addresses</a>
  50. * for additional information. This implementation is intended to validate the following emails:<tt>
  51. * 'barney@example.de', 'barney.rubble@example.com', 'barney-rubble@example.coop', 'barney+rubble@example.com'
  52. * </tt>.
  53. * @param {String} value The email address
  54. * @return {Boolean} true if the RegExp test passed, and false if not.
  55. */
  56. 'email' : function(v){
  57. return email.test(v);
  58. },
  59. /**
  60. * The error text to display when the email validation function returns false. Defaults to:
  61. * <tt>'This field should be an e-mail address in the format "user@example.com"'</tt>
  62. * @type String
  63. */
  64. 'emailText' : 'This field should be an e-mail address in the format "user@example.com"',
  65. /**
  66. * The keystroke filter mask to be applied on email input. See the {@link #email} method for
  67. * information about more complex email validation. Defaults to:
  68. * <tt>/[a-z0-9_\.\-@]/i</tt>
  69. * @type RegExp
  70. */
  71. 'emailMask' : /[a-z0-9_\.\-@\+]/i,
  72. /**
  73. * The function used to validate URLs
  74. * @param {String} value The URL
  75. * @return {Boolean} true if the RegExp test passed, and false if not.
  76. */
  77. 'url' : function(v){
  78. return url.test(v);
  79. },
  80. /**
  81. * The error text to display when the url validation function returns false. Defaults to:
  82. * <tt>'This field should be a URL in the format "http:/'+'/www.example.com"'</tt>
  83. * @type String
  84. */
  85. 'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',
  86. /**
  87. * The function used to validate alpha values
  88. * @param {String} value The value
  89. * @return {Boolean} true if the RegExp test passed, and false if not.
  90. */
  91. 'alpha' : function(v){
  92. return alpha.test(v);
  93. },
  94. /**
  95. * The error text to display when the alpha validation function returns false. Defaults to:
  96. * <tt>'This field should only contain letters and _'</tt>
  97. * @type String
  98. */
  99. 'alphaText' : 'This field should only contain letters and _',
  100. /**
  101. * The keystroke filter mask to be applied on alpha input. Defaults to:
  102. * <tt>/[a-z_]/i</tt>
  103. * @type RegExp
  104. */
  105. 'alphaMask' : /[a-z_]/i,
  106. /**
  107. * The function used to validate alphanumeric values
  108. * @param {String} value The value
  109. * @return {Boolean} true if the RegExp test passed, and false if not.
  110. */
  111. 'alphanum' : function(v){
  112. return alphanum.test(v);
  113. },
  114. /**
  115. * The error text to display when the alphanumeric validation function returns false. Defaults to:
  116. * <tt>'This field should only contain letters, numbers and _'</tt>
  117. * @type String
  118. */
  119. 'alphanumText' : 'This field should only contain letters, numbers and _',
  120. /**
  121. * The keystroke filter mask to be applied on alphanumeric input. Defaults to:
  122. * <tt>/[a-z0-9_]/i</tt>
  123. * @type RegExp
  124. */
  125. 'alphanumMask' : /[a-z0-9_]/i
  126. };
  127. })());