Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with my script in IE.

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
heres my onBlur function to change the first letter of each word to uppercase.

function toUpper(value) {

var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters

var a = value.split(/\s+/g); // split the sentence into an array of words

for (i = 0 ; i < a.length ; i ++ ) {
var parts = a.match(pattern); // just a temp variable to store the fragments in.

var firstLetter = parts[1].toUpperCase();
var restOfWord = parts[2].toLowerCase();

a = firstLetter + restOfWord; // re-assign it back to the array and move on
}

value = a.join(' '); // join it back together
return value;
}

It works fine in fireFox, but in IE if the form fields are empty, i get this error:

### ERROR ###
'1' is null or not an object at line 'var parts = a.match(pattern);'
### ERROR ###

The script carries on with the error, but i need to know how elimate this bug.

Thanks.

 
ok problem fixed .. added:

if (value == "") { return value; }

as the first line of the function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top