Posts Tagged ‘javascript’

Escaping a Plus in Javascript

Friday, September 19th, 2008

I got stuck trying to send a string that contained a “+” via Javascript to a Flash component. Using “escape()” didn’t help as it continued to come through as a space. The workaround I discovered is to use “encodeURIComponent()” instead.

CSS max-width proportional scaling in IE6

Wednesday, March 19th, 2008

The CSS property ‘max-width’ and ‘max-height’ doesn’t work in Internet Explorer 6. Here’s a workaround:

.myclass {
width:expression(this.width > 100 ? (this.height > this.width ? (this.width / this.height) * 100 : 100) : true);
}

What does this do? It executes a bit of javascript within the CSS that goes along the lines of “If the width is greater than 100 pixels then set the width to 100 – unless the height is greater than the width, in which case scale it down to the difference between the width and the height – otherwise just leave it as it is”.

This means that any element that is of .myclass will fit proportionately into a 100×100 area (change the 100 for whatever max-width or max-height you require).

I hope this saves someone from having to figure this out in the future.

Some Handy Regular Expressions

Saturday, February 9th, 2008

At least something
/.+/

A positive integer
/^\d*$/

A decimal number
/^\d*\.?\d/

A valid email address
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

A valid web address
/^(http:\/\/)?\w+([\.-]?\w+)*\w+([\.-]?\w+)*(\.\w+)+(\/)?(\w+)?$/

A hexadecimal colour (6 chars)
/^#?[0-9A-Fa-f]{6}$/

How to use in Javascript:
var testString = "ff0000";
var pattern = /^#?[0-9A-Fa-f]{6}$/;
if (testString.match(pattern)) DoSomething();

How to use in PHP:
$testString = 'ff0000';
$pattern = '/^#?[0-9A-Fa-f]{6}$/';
if (preg_match($pattern, $testString) DoSomething();

How to use in C#:
string testString = "ff0000";
string pattern = @"^#?[0-9A-Fa-f]{6}$";
Match m = Regex.Match(testString, pattern);
if (m.Success) DoSomething();