Two Quick JavaScript Snippets I Use Everyday

Here are some JavaScript snippets that I’ve found in various places across the web that I find highly useful in my day-to-day tasks.

Format Numbers

My users started asking me to show large numbers with commas so it would be easier to tell the difference between 100000 and 1000000 — just glancing you may not see the difference, but with commas — 100,000 and 1,000,000 — it is much easier to scan a page full of numbers and quickly see the differences.

JavaScript has no native functions for formatting numbers, so here’s a great one to include:

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
Illegal XML Characters

Many operations that are performed in SharePoint may be based on data this is not currently displayed. This can include lookups to other lists or filters based on columns in the current list. End-users will sometimes make use of characters that cannot be ported directly into XML because they’ll keep the document from being well-formed. For example, a list contains a choice column and the user enters a single item with an ampersand. If you’re building dynamic queries for SPServices, this will cause your CAML query to be malformed.

Here’s a quick function that checks for the most common illegal XML characters:

function xmlencode(string) {
    return string.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;')
        .replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
}

Just pass in your dynamic query item to this function and use the returned value in the CAML query.

Advertisements

10 thoughts on “Two Quick JavaScript Snippets I Use Everyday

  1. Hi Chris,

    Thanks for the post. I have a field that has a value of BAG® TTM® SSM. how do I use CAML query to get the results back if I filter the results on that field. I tried with ascii code for ® but failed. Any suggestions/help is much appreciated. Thanks.

  2. Hi Chris,

    Thanks for the post. I have to use CAML query to query a field that has special characters such as © ® ™ in it. For e.g the product name is abcd® efg ™. I tried ANSI number but u2u didn’t return any records. How do I use CAML query to query this field? Any suggestions/help is highly appreciated. Thanks.

    1. This is not something I have come across before. Most special characters in SharePoint are dealt with in UNICODE, not ANSI. For example, spaces are represented as 0020. I would look up the unicode value for those special characters and insert that into the query using _x0000_ (i.e. Sample Value would be Sample_x0020_Value). Not sure if this will work though.

  3. For the record, SharePoint has a built-in JavaScript function for encoding, called STSHtmlEncode. Not written like yours (it uses a switch statement), but from what I see both will give the same result.

    1. Hey Christophe,
      Do you know of any good resources that point directly to OOTB SharePoint javascript functions? I’d like to have a list of them, so I can resuse what is already there 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s