jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > Validator.url.js (source view)
Search:
 
Filters
/*!
 * Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
 *
 * This Software is licenced under the LGPL Licence (GNU Lesser General 
 * Public License).
 * In addition to the LGPL Licence the Software is subject to the 
 * following conditions:
 * 
 * 	i	every modification must be public and comunicated to the Author
 * 	ii	every "jsbean" added to this library must be self consistent 
 * 		except for the dependence from jsbeans-x.x.x.js
 * 	iii	copyright notice and this permission notice shall be included 
 * 		in all copies or substantial portions of the Software
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
 
/**
 * Validator extensions for uri (http,https,ftp,file, ...)
 * @for Validator
 * @namespace jsbeans
 * @static
 * */
jsbeans.Validator.url = {
	/**
	 * Default messages for Validator.url extension
	 * @property url.messages
	 * @type JSON
	 * @static
	 * */
	messages: {
		en: "is not a valid url",
		it: "non e' un URL valido"
	},
	/**
	 * Checks if {@param} <code class="param">input</code>'s value contains a string representing an url, optionally matching one of given {@param} <code class="param">protocol_list</code>
	 * @method assertUrl
	 * @param input {DOM} the input to validate
	 * @param [protocol_list] {String} a CSV (Comma Separeted Values) of allowed protocols.<br/>
	 * Note that, by now, validation checks against only the following protocols: file, gopher, news, nntp, telnet, http, ftp, https, ftps, sftp.
	 * @return {boolean} true if <code class="param">input</code>'s value matches one of given <code class="param">protocol_list</code>
	 * @static
	 * */
	assertUrl: function(obj, arg) {
		var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
		if (val == "") {
			return true;
		}
		arg = arg || null;
		if (arg != null) {
			var sp = arg.split(",");
			var res = false;
			for (var i = 0, urlType; urlType = sp[i]; i++) {
				var re = this._getRegExp(urlType);
				if (re != null) {
					if (re.test(val)) {
						// returns true at first match
						return true;
					}
				}
			}
			return false;
		} 
		else {
			return /(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp):\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/.test(val);
		}
	}
	,_getRegExp: function(urlType) {
		var res = null;
		switch (urlType) {
			case "file" : 
				res = /((file:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "ftp" : 
				res = /((ftp:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "ftps" : 
				res = /((ftps:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "gopher" : 
				res = /((gopher:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "http" : 
				res = /((http:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "https" :
				res = /((https:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "news" : 
				res = /((news:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "nntp" : 
				res = /((nntp:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "sftp" : 
				res = /((sftp:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
			case "telnet" : 
				res = /((telnet:\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&amp;%_\.\/-~-]*)?/;
				break;
		}
		return res;
	}
};

Copyright © 2016 Francesco Mele. All rights reserved.