Regular expression for URL validation (in JavaScript)

In the accepted answer bobince got it right: validating only the scheme name, ://, and spaces and double quotes is usually enough. Here is how the validation can be implemented in JavaScript: var url=”http://www.google.com”; var valid = /^(ftp|http|https):\/\/[^ “]+$/.test(url); // true or var r = /^(ftp|http|https):\/\/[^ “]+$/; r.test(‘http://www.goo le.com’); // false or var url=”http:www.google.com”; var …

Read more

How do I style the HTML form validation error messages with CSS?

Currently, there is no way to style those little validation tooltips. The only other option, which is what I have chosen to do, is to disable the browser validation all together for now and rely on my own client-side validation scripts. According to this article: http://blog.oldworld.fr/index.php?post/2010/11/17/HTML5-Forms-Validation-in-Firefox-4 “The simplest way to opt out is to add …

Read more

How does Page.IsValid work?

Validation occurs after Page_Load, but before event handlers (See http://msdn.microsoft.com/en-us/library/ms178472(v=VS.100).aspx). If your button does not cause validation, you must manually fire Page.Validate. You may not interrogate Page.IsValid until after (1) you have called Page.Validate or (2) a control that causes validation was the source of/included in a postback. If you require validation to occur before …

Read more

Validating DataAnnotations with Validator class

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Persona), typeof(Persona_Validation)), typeof(Persona)); ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, context, results, true);