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=””; var valid = /^(ftp|http|https):\/\/[^ “]+$/.test(url); // true or var r = /^(ftp|http|https):\/\/[^ “]+$/; r.test(‘’); // false or var url=””; var … Read more

An improved isNumeric() function?

In my opinion, if it’s an array then its not numeric. To alleviate this problem, I added a check to discount arrays from the logic You can have that problem with any other object as well, for example {toString:function(){return “1.2”;}}. Which objects would you think were numeric? Number objects? None? Instead of trying to blacklist … 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: “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 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: 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);