diff --git a/clientside_validation.js b/clientside_validation.js index aca6cc8..49b9be8 100644 --- a/clientside_validation.js +++ b/clientside_validation.js @@ -1264,6 +1264,27 @@ }, jQuery.format('Please fill in at least on of the fields')); // Support for phone + jQuery.validator.addMethod("drupalURL", function(value, element, param) { + var url = param; + var result = false; + jQuery.ajax({ + 'url': Drupal.settings.basePath + 'clientside_validation/drupalURL', + 'type': "POST", + 'data': { + 'value': value, + 'url': url + }, + 'dataType': 'json', + 'async': false, + 'success': function(res){ + result = res; + } + }); + return result.result; + + }, jQuery.format('Please fill in a valid url')); + + // Support for phone jQuery.validator.addMethod("phone", function(value, element, param) { var country_code = param; var result = false; diff --git a/clientside_validation.module b/clientside_validation.module index 99fefce..f17ed2e 100644 --- a/clientside_validation.module +++ b/clientside_validation.module @@ -172,6 +172,14 @@ function clientside_validation_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + + $items['clientside_validation/drupalURL'] = array( + 'title' => 'Clientside validation validate url callback', + 'page callback' => '_clientside_validation_url_validation_callback', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -1569,3 +1577,30 @@ function clientside_validation_library_alter(&$libraries, $module) { $libraries['jquery.form']['version'] = '3.09'; } } + +/** + * Check that a path is valid and accessible for internal and aliased paths, also if the path is an + * external url, it is validated according to RFC 2396. + * + * @param $path + * The path to check is valid. + */ +function _clientside_validation_url_validation_callback($path) { + // Check if the url is valid according to RFC 2396. + if (filter_var($path, FILTER_VALIDATE_URL)) { + return TRUE; + } + // Checks a path exists and the current user has access to it. + elseif (drupal_valid_path($path)) { + return TRUE; + } + else { + // Check if the aliased path, exists and the current user has access to it. + $lookup_path = drupal_lookup_path('source', $path); + if (drupal_valid_path($lookup_path)) { + return TRUE; + } + } + + return FALSE; +}