/src/jquery.valid.js

https://github.com/vincentferniot/jquery-utils · JavaScript · 95 lines · 47 code · 2 blank · 46 comment · 6 complexity · 67836a585d6310bb7c11aa754631ede7 MD5 · raw file

  1. /*
  2. jQuery valid - 0.1
  3. http://code.google.com/p/jquery-utils
  4. (c) Maxime Haineault <haineault@gmail.com>
  5. http://haineault.com
  6. MIT License (http://www.opensource.org/licenses/mit-license.php)
  7. +-----------------+---+---+-----------------------------------------------------------+
  8. | Validation | I | T | Description |
  9. +-----------------+---+---+-----------------------------------------------------------+
  10. | integer | x | x | true if input is a valid integer |
  11. | numeric | x | x | true if input is a valid numeric value (float or integer) |
  12. | boolean | x | x | true if input is a valid boolean (1 or 0) |
  13. | alphanum | x | x | true if input is a valid alphanumeric value |
  14. | inrange | | | true if input is within given range |
  15. | minlength | | | true if input is at least or equal the specified length |
  16. | maxlength | | | true if input is less or equal than specified length |
  17. | net.email | x | x | true if input is a valid email address |
  18. | net.ip | x | x | true if input is a valid IPv4 address |
  19. | net.mac | x | x | true if input is a valid MAC address |
  20. | net.uri | | | true if input is a valid URL (http, ftp, svn, ...) |
  21. | date | | | true if input is a valid date (ISO) |
  22. | creditcard | | | true if input is a valid credit card number |
  23. | mysql.date | x | | true if input is a valid MySQL date |
  24. | mysql.datetime | x | | true if input is a valid MySQL datetime |
  25. | mysql.time | x | | true if input is a valid MySQL time |
  26. | mysql.timestamp | | | true if input is a valid MySQL timestamp |
  27. | ca.date | x | | true if input is a valid Canadian date |
  28. | ca.time | | | true if input is a valid Canadian time |
  29. | ca.currency | | | true if input is a valid Canadian currency |
  30. | ca postalCode | | | true if input is a valid Canadian postalCode |
  31. | ca.phoneNumber | | | true if input is a valid Canadian phoneNumber |
  32. | ca.ssn | | | true if input is a valid Canadian Social Security Number |
  33. | ca.sin | | | true if input is a valid Canadian Social Insurange Number |
  34. +-----------------+---+---+-----------------------------------------------------------+
  35. */
  36. (function($){
  37. $.extend({
  38. validator: {},
  39. isValid: function(validation, i, args) {
  40. var parts = validation.split('.');
  41. // namespaced
  42. if (parts[1] && $.validator[parts[0]] && $.validator[parts[0]][parts[1]]) {
  43. return $.validator[parts[0]][parts[1]](i, args);
  44. }
  45. else if ($.isFunction($.validator[parts[0]])) {
  46. return $.validator[parts[0]](i, args);
  47. }
  48. }
  49. });
  50. $.extend($.validator, {
  51. // Types
  52. integer: function(i, args) { return /(^-?\d\d*$)/.test(i); },
  53. numeric: function(i, args) { return /^[-+]?[0-9]\d*\.?[0-9]*$/.test(i); },
  54. 'boolean':function(i, args) { return (args && args.strict)? /^0|1|true|false$/.test(i): /^0|1$/.test(i); },
  55. alphanum: function(i, args) { return /^[a-zA-Z0-9.\-_]+$/.test(i); },
  56. CSShex: function(i, args) { return /^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?$/.test(i); },
  57. // Networking
  58. net: {
  59. // http://regular-expressions.info/email.html
  60. email: function(i, args) { return /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b$/.test(i); },
  61. ip: function(i){ return /^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4] \d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4] \d|25[0-5])$/.test(i); },
  62. MACaddress: function(i){ return /^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/.test(i); }
  63. },
  64. // MySQL
  65. mysql: {
  66. 'date': function() { return /^\d{4}-\d{2}-\d{2}/.test(i); },
  67. datetime: function() { return /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/.test(i); },
  68. timestamp: function() {}, // default Mysql 5+
  69. time: function() { return /^\d{2}:\d{2}:\d{2}/.test(i); }
  70. },
  71. us: {
  72. ssn: function(i, args) { return /^\d{3}-\d{2}-\d{4}$/.test(i); },
  73. phoneNumber: function(i, args) { return /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/.test(i); },
  74. zipCode: function(i, args) { return /^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$/.test(i); },
  75. currency: function(i, args) { return /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(i); }
  76. },
  77. // Locals
  78. ca: {
  79. // (yyyy-mm-dd || yyyy/mm/dd || yyyy.mm.dd)
  80. 'date': function(i, args) { return /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/.test(i); },
  81. // (HH:MM || HH:MM:SS || HH:MM:SS.mmm)
  82. time: function(i, args) { return /^([0]?[1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/.test(i); },
  83. currency: function() {},
  84. postalCode: function() {},
  85. phoneNumber: function() {},
  86. ssn: function() {},
  87. sin: function() {}
  88. }
  89. });
  90. })(jQuery);