| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: |
|
| 15: |
|
| 16: | use LucidFrame\Core\Validation;
|
| 17: |
|
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: |
|
| 24: | function __validation_init()
|
| 25: | {
|
| 26: | $validationMessages = array(
|
| 27: | 'default' => "'%s' needs to be revised.",
|
| 28: | 'mandatory' => "'%s' is required.",
|
| 29: | 'mandatoryOne' => "'%s' must be entered/selected at least one.",
|
| 30: | 'mandatoryAll' => "'%s' is required. All must be entered/selected.",
|
| 31: | 'notAllowZero' => "'%s' should not be zero.",
|
| 32: | 'alphaNumeric' => "'%s' should contain only letters and numbers.",
|
| 33: | 'alphaNumericSpace' => "'%s' should contain only letters, numbers and spaces.",
|
| 34: | 'alphaNumericDash' => "'%s' should contain only letters, numbers and dashes.",
|
| 35: | 'numeric' => "'%s' should be a number.",
|
| 36: | 'numericSpace' => "'%s' should contain only numbers and spaces.",
|
| 37: | 'numericDash' => "'%s' should contain only numbers and dashes. It should not start or end with a dash.",
|
| 38: | 'username' => "'%s' should contain only letters, numbers, periods, underscores and dashes.",
|
| 39: | 'naturalNumber' => "'%s' should be a positive integer. It is not allowed zero.",
|
| 40: | 'wholeNumber' => "'%s' should be a positive integer.",
|
| 41: | 'integer' => "'%s' should be a positive or negative integer.",
|
| 42: | 'rationalNumber' => "'%s' should be an integer or decimal.",
|
| 43: | 'positiveRationalNumber' => "'%s' should be a positive integer or decimal.",
|
| 44: | 'email' => "'%s' should be a valid format, e.g., username@example.com",
|
| 45: | 'domain' => "'%s' should be a valid domain name with letters, numbers and dash only.",
|
| 46: | 'url' => "'%s' should be a valid website address, e.g., http://www.example.com",
|
| 47: | 'exactLength' => "'%s' should have exact length of %d.",
|
| 48: | 'min' => "'%s' should be greater than or equal to %d.",
|
| 49: | 'max' => "'%s' should be less than or equal to %d.",
|
| 50: | 'minLength' => "'%s' should have at least %d letters.",
|
| 51: | 'maxLength' => "'%s' should not exceed %d letters.",
|
| 52: | 'between' => "'%s' should be between %d and %d.",
|
| 53: | 'fileMaxSize' => "'%s' cannot exceed the maximum allowed upload size %dMB.",
|
| 54: | 'fileMaxWidth' => "'%s' cannot exceed the maximum allowed width %dpx.",
|
| 55: | 'fileMaxHeight' => "'%s' cannot exceed the maximum allowed height %dpx.",
|
| 56: | 'fileMaxDimension' => "'%s' cannot exceed the maximum allowed dimension %dpx%dpx.",
|
| 57: | 'fileExactDimension' => "'%s' should have the dimension %dx%dpx.",
|
| 58: | 'fileExtension' => "'%s' must be one of the file types: %s.",
|
| 59: | 'date' => "'%s' should be valid for the date format '%s'.",
|
| 60: | 'time' => "'%s' should be valid for the time format '%s'.",
|
| 61: | 'datetime' => "'%s' should be valid for the date/time format '%s %s'.",
|
| 62: | 'unique' => "'%s' already exists. Please try another one.",
|
| 63: | 'mmPhone' => "'%s' should be a valid Myanmar phone number.",
|
| 64: | 'custom' => "'%s' should be a valid format."
|
| 65: | );
|
| 66: |
|
| 67: | if (function_exists('__validation_messages')) {
|
| 68: | $validationMessages = array_merge($validationMessages, __validation_messages());
|
| 69: | }
|
| 70: |
|
| 71: | $i18nEnabled = function_exists('_t');
|
| 72: |
|
| 73: | foreach ($validationMessages as $key => $msg) {
|
| 74: | $validationMessages[$key] = ($i18nEnabled) ? _t($msg) : $msg;
|
| 75: | }
|
| 76: |
|
| 77: | Validation::set('messages', $validationMessages);
|
| 78: | }
|
| 79: |
|
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: |
|
| 86: | function validation_set($key, $value = null)
|
| 87: | {
|
| 88: | Validation::set($key, $value);
|
| 89: | }
|
| 90: |
|
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: |
|
| 96: | function validation_get($key)
|
| 97: | {
|
| 98: | return Validation::get($key);
|
| 99: | }
|
| 100: |
|
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: |
|
| 112: | function validation_check($validations, $data = [], $type = Validation::TYPE_MULTI)
|
| 113: | {
|
| 114: | return Validation::check($validations, $data, $type);
|
| 115: | }
|
| 116: |
|
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: |
|
| 125: | function validation_addError($id, $msg)
|
| 126: | {
|
| 127: | Validation::addError($id, $msg);
|
| 128: | }
|
| 129: |
|
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: |
|
| 135: | function validate_mandatory($value)
|
| 136: | {
|
| 137: | if (is_array($value) && count($value) == 0) {
|
| 138: | return false;
|
| 139: | }
|
| 140: |
|
| 141: | if (is_array($value) && isset($value['name']) && empty($value['name'])) {
|
| 142: | return false;
|
| 143: | }
|
| 144: |
|
| 145: | if (empty($value) && $value != '0') {
|
| 146: | return false;
|
| 147: | }
|
| 148: |
|
| 149: | return (is_array($value)) ? true : preg_match('/[^\s]+/', $value);
|
| 150: | }
|
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: |
|
| 156: | function validate_mandatoryOne($value)
|
| 157: | {
|
| 158: | if (is_array($value)) {
|
| 159: | $value = array_unique($value);
|
| 160: | $empty = true;
|
| 161: | foreach ($value as $v) {
|
| 162: | if (preg_match('/[^\s]+/', $v)) {
|
| 163: |
|
| 164: | $empty = false;
|
| 165: | }
|
| 166: | }
|
| 167: | return !$empty;
|
| 168: | } else {
|
| 169: | return preg_match('/[^\s]+/', $value);
|
| 170: | }
|
| 171: | }
|
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: |
|
| 177: | function validate_mandatoryAll($value)
|
| 178: | {
|
| 179: | if (is_array($value)) {
|
| 180: | $value = array_unique($value);
|
| 181: | foreach ($value as $v) {
|
| 182: | if (preg_match('/[\s]+/', $v)) {
|
| 183: |
|
| 184: | return false;
|
| 185: | }
|
| 186: | }
|
| 187: | return true;
|
| 188: | } else {
|
| 189: | return preg_match('/[^\s]+/', $value);
|
| 190: | }
|
| 191: | }
|
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: |
|
| 197: | function validate_notAllowZero($value)
|
| 198: | {
|
| 199: | $value = trim($value);
|
| 200: |
|
| 201: | return ($value == '0' || $value == 0) ? false : true;
|
| 202: | }
|
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: |
|
| 208: | function validate_alphaNumeric($value)
|
| 209: | {
|
| 210: | $value = trim($value);
|
| 211: | if ($value == '') {
|
| 212: | return true;
|
| 213: | }
|
| 214: |
|
| 215: |
|
| 216: | return preg_match('/^[A-Za-z0-9]+$/', $value);
|
| 217: | }
|
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: |
|
| 223: | function validate_alphaNumericSpace($value)
|
| 224: | {
|
| 225: | $value = trim($value);
|
| 226: | if ($value == '') {
|
| 227: | return true;
|
| 228: | }
|
| 229: |
|
| 230: |
|
| 231: | return preg_match('/^[A-Za-z0-9 ]+$/', $value);
|
| 232: | }
|
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: |
|
| 238: | function validate_alphaNumericDash($value)
|
| 239: | {
|
| 240: | $value = trim($value);
|
| 241: | if ($value == '') {
|
| 242: | return true;
|
| 243: | }
|
| 244: |
|
| 245: |
|
| 246: | return preg_match('/^[A-Za-z0-9\-]+$/', $value);
|
| 247: | }
|
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: |
|
| 253: | function validate_numeric($value)
|
| 254: | {
|
| 255: | $value = trim($value);
|
| 256: | if ($value == '') {
|
| 257: | return true;
|
| 258: | }
|
| 259: |
|
| 260: |
|
| 261: | return is_numeric($value);
|
| 262: | }
|
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: |
|
| 268: | function validate_numericDash($value)
|
| 269: | {
|
| 270: | if (is_numeric($value) && strlen($value) == 1) {
|
| 271: | return true;
|
| 272: | }
|
| 273: |
|
| 274: | if (empty($value)) {
|
| 275: | return true;
|
| 276: | }
|
| 277: |
|
| 278: | return preg_match('/^([0-9])+([0-9\-])*([0-9])+$/', $value);
|
| 279: | }
|
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: |
|
| 285: | function validate_numericSpace($value)
|
| 286: | {
|
| 287: | if (is_numeric($value) && strlen($value) == 1) {
|
| 288: | return true;
|
| 289: | }
|
| 290: |
|
| 291: | if (empty($value)) {
|
| 292: | return true;
|
| 293: | }
|
| 294: |
|
| 295: | return preg_match('/^[0-9 ]+$/', $value);
|
| 296: | }
|
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: |
|
| 302: | function validate_username($value)
|
| 303: | {
|
| 304: | $value = trim($value);
|
| 305: | if ($value == '') {
|
| 306: | return true;
|
| 307: | }
|
| 308: |
|
| 309: |
|
| 310: | return preg_match('/^([A-Za-z])+([A-Za-z0-9_\-\.])*([A-Za-z0-9])+$/', $value);
|
| 311: | }
|
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: |
|
| 319: | function validate_naturalNumber($value)
|
| 320: | {
|
| 321: | $value = trim($value);
|
| 322: | if ($value == '') {
|
| 323: | return true;
|
| 324: | }
|
| 325: |
|
| 326: | return preg_match('/^[1-9][0-9]*$/', $value);
|
| 327: | }
|
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: |
|
| 334: | function validate_wholeNumber($value)
|
| 335: | {
|
| 336: | $value = trim($value);
|
| 337: | if ($value == '') {
|
| 338: | return true;
|
| 339: | }
|
| 340: |
|
| 341: | return preg_match('/^(?:0|[1-9][0-9]*)$/', $value);
|
| 342: | }
|
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: |
|
| 349: | function validate_integer($value)
|
| 350: | {
|
| 351: | $value = trim($value);
|
| 352: | if ($value == '') {
|
| 353: | return true;
|
| 354: | }
|
| 355: |
|
| 356: | return preg_match('/^[-]?(?:0|[1-9][0-9]*)$/', $value);
|
| 357: | }
|
| 358: | |
| 359: | |
| 360: | |
| 361: | |
| 362: | |
| 363: |
|
| 364: | function validate_rationalNumber($value)
|
| 365: | {
|
| 366: | $value = trim($value);
|
| 367: | if ($value == '') {
|
| 368: | return true;
|
| 369: | }
|
| 370: |
|
| 371: | return preg_match('/^[-]?[0-9]*[\.]?[0-9]+$/', $value);
|
| 372: | }
|
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: |
|
| 379: | function validate_positiveRationalNumber($value)
|
| 380: | {
|
| 381: | $value = trim($value);
|
| 382: | if ($value == '') {
|
| 383: | return true;
|
| 384: | }
|
| 385: |
|
| 386: | return preg_match('/^[0-9]*[\.]?[0-9]+$/', $value);
|
| 387: | }
|
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: |
|
| 393: | function validate_email($value)
|
| 394: | {
|
| 395: | $value = trim($value);
|
| 396: | if ($value == '') {
|
| 397: | return true;
|
| 398: | }
|
| 399: |
|
| 400: | return preg_match('/^[A-Za-z0-9]([A-Za-z0-9]|_|\.|\-)*@([a-z0-9]|\.|\-)+\.[a-z]{2,4}$/', $value);
|
| 401: | }
|
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: |
|
| 407: | function validate_domain($value)
|
| 408: | {
|
| 409: | if (empty($value)) {
|
| 410: | return true;
|
| 411: | }
|
| 412: |
|
| 413: | return preg_match('/^([a-z])+([a-z0-9\-])*([a-z0-9])+$/i', $value);
|
| 414: | }
|
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: |
|
| 420: | function validate_url($value)
|
| 421: | {
|
| 422: | if (empty($value)) {
|
| 423: | return true;
|
| 424: | }
|
| 425: |
|
| 426: | $value = rtrim($value, '/');
|
| 427: |
|
| 428: |
|
| 429: | preg_match("/^((http|https|ftp):\/\/)?([^\/]+)/i", $value, $matches);
|
| 430: | $host = $matches[3];
|
| 431: | $hostParts = explode('.', $host);
|
| 432: |
|
| 433: | if (strstr($host, '@') !== false) {
|
| 434: | return false;
|
| 435: | }
|
| 436: |
|
| 437: | if (preg_match('/^(w+)$/i', $hostParts[0], $matches) && strlen($matches[0]) != 3) {
|
| 438: | return false;
|
| 439: | }
|
| 440: |
|
| 441: | return preg_match('/^((http|https|ftp):\/\/)?([a-z0-9_\-]+\.)+([a-z]{2,13})(\/[.^\S]+)*$/', $value);
|
| 442: | }
|
| 443: | |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: |
|
| 449: | function validate_exactLength($value, $length)
|
| 450: | {
|
| 451: | if (is_array($value)) {
|
| 452: | return count($value) == $length;
|
| 453: | }
|
| 454: |
|
| 455: | return mb_strlen($value) == $length;
|
| 456: | }
|
| 457: | |
| 458: | |
| 459: | |
| 460: | |
| 461: | |
| 462: |
|
| 463: | function validate_minLength($value, $min)
|
| 464: | {
|
| 465: | return mb_strlen($value) >= $min;
|
| 466: | }
|
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: |
|
| 473: | function validate_maxLength($value, $max)
|
| 474: | {
|
| 475: | $length = mb_strlen($value);
|
| 476: | return ($length <= $max);
|
| 477: | }
|
| 478: | |
| 479: | |
| 480: | |
| 481: | |
| 482: | |
| 483: |
|
| 484: | function validate_min($value, $min)
|
| 485: | {
|
| 486: | return $value >= $min;
|
| 487: | }
|
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: |
|
| 494: | function validate_max($value, $max)
|
| 495: | {
|
| 496: | return $value <= $max;
|
| 497: | }
|
| 498: | |
| 499: | |
| 500: | |
| 501: | |
| 502: | |
| 503: | |
| 504: |
|
| 505: | function validate_between($value, $min, $max)
|
| 506: | {
|
| 507: | return $value >= $min && $value <= $max;
|
| 508: | }
|
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: | |
| 515: | |
| 516: |
|
| 517: | function validate_custom($value, $pattern)
|
| 518: | {
|
| 519: | if (empty($value) && $value != '0') {
|
| 520: | return true;
|
| 521: | }
|
| 522: |
|
| 523: | return preg_match($pattern, $value);
|
| 524: | }
|
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | |
| 530: |
|
| 531: | function validate_fileExtension($value, $extensions = array('jpg', 'jpeg', 'png', 'gif'))
|
| 532: | {
|
| 533: | if (!is_array($value)) {
|
| 534: | return true;
|
| 535: | }
|
| 536: |
|
| 537: | if (!file_exists($value['tmp_name'])) {
|
| 538: | return true;
|
| 539: | }
|
| 540: |
|
| 541: | if (empty($value['name'])) {
|
| 542: | return true;
|
| 543: | }
|
| 544: |
|
| 545: | $ext = explode('.', $value['name']);
|
| 546: | $ext = strtolower(end($ext));
|
| 547: |
|
| 548: | return in_array($ext, $extensions);
|
| 549: | }
|
| 550: | |
| 551: | |
| 552: | |
| 553: | |
| 554: | |
| 555: |
|
| 556: | function validate_fileMaxSize($value, $maxSize = null)
|
| 557: | {
|
| 558: | if (!is_array($value)) {
|
| 559: | return true;
|
| 560: | }
|
| 561: |
|
| 562: | if (is_null($maxSize)) {
|
| 563: | return true;
|
| 564: | }
|
| 565: |
|
| 566: | $fileSize = $value['size'];
|
| 567: | $maxSize = $maxSize * 1024 * 1024;
|
| 568: |
|
| 569: | return $fileSize <= $maxSize;
|
| 570: | }
|
| 571: | |
| 572: | |
| 573: | |
| 574: | |
| 575: | |
| 576: | |
| 577: | |
| 578: | |
| 579: |
|
| 580: | function validate_fileMaxDimension($value, $maxWidth, $maxHeight)
|
| 581: | {
|
| 582: | if (!is_array($value)) {
|
| 583: | return true;
|
| 584: | }
|
| 585: |
|
| 586: | if (!file_exists($value['tmp_name'])) {
|
| 587: | return true;
|
| 588: | }
|
| 589: |
|
| 590: | list($width, $height) = getimagesize($value['tmp_name']);
|
| 591: | return $width <= $maxWidth && $height <= $maxHeight;
|
| 592: | }
|
| 593: | |
| 594: | |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: |
|
| 602: | function validate_fileExactDimension($value, $width, $height)
|
| 603: | {
|
| 604: | if (!is_array($value)) {
|
| 605: | return true;
|
| 606: | }
|
| 607: |
|
| 608: | if (!file_exists($value['tmp_name'])) {
|
| 609: | return true;
|
| 610: | }
|
| 611: |
|
| 612: | list($w, $h) = getimagesize($value['tmp_name']);
|
| 613: |
|
| 614: | return $w == $width && $h == $height;
|
| 615: | }
|
| 616: | |
| 617: | |
| 618: | |
| 619: | |
| 620: | |
| 621: | |
| 622: | |
| 623: |
|
| 624: | function validate_fileMaxWidth($value, $maxWidth)
|
| 625: | {
|
| 626: | if (!is_array($value)) {
|
| 627: | return true;
|
| 628: | }
|
| 629: |
|
| 630: | if (!file_exists($value['tmp_name'])) {
|
| 631: | return true;
|
| 632: | }
|
| 633: |
|
| 634: | list($width, $height) = getimagesize($value['tmp_name']);
|
| 635: |
|
| 636: | return $width <= $maxWidth;
|
| 637: | }
|
| 638: | |
| 639: | |
| 640: | |
| 641: | |
| 642: | |
| 643: | |
| 644: | |
| 645: |
|
| 646: | function validate_fileMaxHeight($value, $maxHeight)
|
| 647: | {
|
| 648: | if (!is_array($value)) {
|
| 649: | return true;
|
| 650: | }
|
| 651: |
|
| 652: | if (!file_exists($value['tmp_name'])) {
|
| 653: | return true;
|
| 654: | }
|
| 655: |
|
| 656: | list($width, $height) = getimagesize($value['tmp_name']);
|
| 657: |
|
| 658: | return $height <= $maxHeight;
|
| 659: | }
|
| 660: | |
| 661: | |
| 662: | |
| 663: | |
| 664: | |
| 665: |
|
| 666: | function validate_ip($value, $type = 'both')
|
| 667: | {
|
| 668: | $type = strtolower($value);
|
| 669: | $flags = 0;
|
| 670: | if ($type === 'v4' || $type === 'ipv4') {
|
| 671: | $flags = FILTER_FLAG_IPV4;
|
| 672: | }
|
| 673: |
|
| 674: | if ($type === 'v6' || $type === 'ipv6') {
|
| 675: | $flags = FILTER_FLAG_IPV6;
|
| 676: | }
|
| 677: |
|
| 678: | return (boolean)filter_var($value, FILTER_VALIDATE_IP, array('flags' => $flags));
|
| 679: | }
|
| 680: | |
| 681: | |
| 682: | |
| 683: | |
| 684: | |
| 685: | |
| 686: | |
| 687: | |
| 688: | |
| 689: | |
| 690: | |
| 691: | |
| 692: |
|
| 693: | function validate_date($value, $format = 'y-m-d')
|
| 694: | {
|
| 695: | if (empty($value)) {
|
| 696: | return true;
|
| 697: | }
|
| 698: |
|
| 699: | $value = trim($value);
|
| 700: | $format = strtolower($format);
|
| 701: | $separators = array('/', '-', '.');
|
| 702: | $sepGroup = '([-\/.])';
|
| 703: | $cleanFormat = preg_replace('/'.$sepGroup.'/', '', $format);
|
| 704: |
|
| 705: | if (in_array($cleanFormat, array('dmy', 'mdy'))) {
|
| 706: | $pattern = '/^([\d]{1,2})'.$sepGroup.'([\d]{1,2})'.$sepGroup.'([\d]{4})$/';
|
| 707: | } else {
|
| 708: | $pattern = '/^([\d]{4})'.$sepGroup.'([\d]{1,2})'.$sepGroup.'([\d]{1,2})$/';
|
| 709: | }
|
| 710: |
|
| 711: | if ($pattern && preg_match_all($pattern, $value, $matches)) {
|
| 712: | if ($matches[2][0] != $matches[4][0]) {
|
| 713: | return false;
|
| 714: | }
|
| 715: |
|
| 716: | if (!in_array($matches[2][0], $separators)) {
|
| 717: | return false;
|
| 718: | }
|
| 719: |
|
| 720: | $sep = $matches[2][0];
|
| 721: | $dt = explode($sep, $value);
|
| 722: | $format = str_split($cleanFormat);
|
| 723: | $ft = array_flip($format);
|
| 724: | $y = $dt[$ft['y']];
|
| 725: | $m = $dt[$ft['m']];
|
| 726: | $d = $dt[$ft['d']];
|
| 727: |
|
| 728: | return checkdate($m, $d, $y);
|
| 729: | }
|
| 730: |
|
| 731: | return false;
|
| 732: | }
|
| 733: | |
| 734: | |
| 735: | |
| 736: | |
| 737: | |
| 738: | |
| 739: | |
| 740: | |
| 741: | |
| 742: | |
| 743: | |
| 744: | |
| 745: | |
| 746: | |
| 747: | |
| 748: |
|
| 749: | function validate_time($value, $timeFormat = 'both')
|
| 750: | {
|
| 751: | if (empty($value)) {
|
| 752: | return true;
|
| 753: | }
|
| 754: |
|
| 755: | $value = trim($value);
|
| 756: | $regex = array(
|
| 757: | '24' => '/^([01]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/',
|
| 758: | '12' => '/^(0?[0-9]|1[0-2]):([0-5][0-9])(:[0-5][0-9])?\s*(am|pm)$/i'
|
| 759: | );
|
| 760: |
|
| 761: | if (!in_array($timeFormat, array('both', '12', '24'))) {
|
| 762: | $timeFormat = 'both';
|
| 763: | }
|
| 764: |
|
| 765: | if ($timeFormat === 'both') {
|
| 766: | $test = $regex;
|
| 767: | } else {
|
| 768: | $test = array($regex[$timeFormat]);
|
| 769: | }
|
| 770: |
|
| 771: | foreach ($test as $pattern) {
|
| 772: | if (preg_match($pattern, $value)) {
|
| 773: | return true;
|
| 774: | }
|
| 775: | }
|
| 776: |
|
| 777: | return false;
|
| 778: | }
|
| 779: | |
| 780: | |
| 781: | |
| 782: | |
| 783: | |
| 784: | |
| 785: | |
| 786: | |
| 787: | |
| 788: | |
| 789: | |
| 790: | |
| 791: | |
| 792: |
|
| 793: | function validate_datetime($value, $dateFormat = 'y-m-d', $timeFormat = 'both')
|
| 794: | {
|
| 795: | if (empty($value)) {
|
| 796: | return true;
|
| 797: | }
|
| 798: |
|
| 799: | $value = trim($value);
|
| 800: | $generalPattern = '/^([\d]{1,4}[-\/.][\d]{1,2}[-\/.][\d]{1,4})(\s+.{4,}\s*(am|pm)?)$/i';
|
| 801: | if (preg_match_all($generalPattern, $value, $matches)) {
|
| 802: | $date = $matches[1][0];
|
| 803: | $time = $matches[2][0];
|
| 804: | return validate_date($date, $dateFormat) && validate_time($time, $timeFormat);
|
| 805: | } else {
|
| 806: | return false;
|
| 807: | }
|
| 808: | }
|
| 809: | |
| 810: | |
| 811: | |
| 812: | |
| 813: | |
| 814: | |
| 815: | |
| 816: | |
| 817: |
|
| 818: | function validate_unique($value, $table, $field, $id = 0)
|
| 819: | {
|
| 820: | $value = strtolower($value);
|
| 821: | if (empty($value)) {
|
| 822: | return true;
|
| 823: | }
|
| 824: |
|
| 825: | $qb = db_count($table)
|
| 826: | ->where()
|
| 827: | ->condition($field, $value);
|
| 828: |
|
| 829: | if ($id) {
|
| 830: | $qb->condition('id !=', $id);
|
| 831: | }
|
| 832: |
|
| 833: | return $qb->fetch() ? false : true;
|
| 834: | }
|
| 835: | |
| 836: | |
| 837: | |
| 838: | |
| 839: |
|
| 840: | function validate_mmPhone($value)
|
| 841: | {
|
| 842: | if (empty($value)) {
|
| 843: | return true;
|
| 844: | }
|
| 845: |
|
| 846: | $pattern = '/^';
|
| 847: | $pattern .= '((\+?959|\(\+?95\)9|^09)([2-9]\d{8})$)|';
|
| 848: | $pattern .= '((\+?959|\(\+?95\)9|^09)(5\d{6})$)|';
|
| 849: | $pattern .= '((\+?95[1-8]|\(\+?95\)[1-8]|^0[1-8])\d{6,7}$)';
|
| 850: | $pattern .= '$/';
|
| 851: |
|
| 852: | $value = str_replace(['-', ' '], '', $value);
|
| 853: |
|
| 854: | return preg_match($pattern, $value);
|
| 855: | }
|
| 856: | |