URLValidator

Tagged:  

In case somebody needs it. Here's a URLValidator. I'm sure there are plenty out there, but it was quick to write, so here it is.

package com.nbilyk.flex.validators {
	import mx.validators.ValidationResult;
	import mx.validators.Validator;
 
	public class URLValidator extends Validator {
		public function URLValidator() {
			super();
		}
 
		private var _invalidUrlError:String = "This is an invalid URL.";
 
		[Inspectable(category="Errors", defaultValue="null")]
 
		/**
		 *  Error message when a string is not a valid url. 
		 *  @default "This is an invalid url."
		 */
		public function get invalidUrlError():String {
			return _invalidUrlError;
		}
		public function set invalidUrlError(value:String):void {
			_invalidUrlError = value;
		}
 
		override protected function doValidation(value:Object):Array {
			var results:Array = super.doValidation(value);
			if (!isUrl(value.toString())) {
				results.push(new ValidationResult(true, "", "invalidUrl", invalidUrlError));   
			}
			return results;
		}
		public static function isUrl(s:String):Boolean {
			var regexp:RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
			return regexp.test(s);
		}
	}
}

The RegExp I just got from a quick google search: http://snippets.dzone.com/posts/show/452

Thx a lot :)

I think your validator accepts the following put as correct url:
http://hotmail

Good, because that is a valid url. For example, localhost and intranet sites don't need a dot extension.
Drupal thinks it's a valid url too, as you can see by the auto-linking.