<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gabe Hilado&#039;s SharePoint &#38; ASP.NET Blog &#187; JavaScript</title>
	<atom:link href="http://spdeveloper.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://spdeveloper.net</link>
	<description>Microsoft, SharePoint, ASP.NET, Software Solutions</description>
	<lastBuildDate>Thu, 29 Sep 2011 15:13:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Web Part &#8220;Error Creating Control&#8221; in SharePoint Designer</title>
		<link>http://spdeveloper.net/2011/08/web-part-error-creating-control-spd/</link>
		<comments>http://spdeveloper.net/2011/08/web-part-error-creating-control-spd/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 00:34:28 +0000</pubDate>
		<dc:creator>Gabe Hilado</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SharePoint Designer]]></category>
		<category><![CDATA[Web Parts]]></category>

		<guid isPermaLink="false">http://spdeveloper.net/?p=478</guid>
		<description><![CDATA[When you are developing custom SharePoint web parts using SharePoint Object Model, it’s not enough that you can properly render contents in “Browse mode”.  Your web part must also take into account when the page it is attached to goes into “Edit mode”. In this blog post, I enumerate the different states the web part [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Error-Creating-Control.png"></a>When you are developing custom SharePoint web parts using SharePoint Object Model, it’s not enough that you can properly render contents in “<strong>Browse mode</strong>”.  Your web part must also take into account when the page it is attached to goes into “<strong>Edit mode</strong>”. In this <a href="http://spdeveloper.net/2009/12/programmatically-check-the-web-part-state/">blog post</a>, I enumerate the different states the web part can be in in any given time. Sometimes, you would want to limit the functionality of the web part when it is not in “Browse mode”. And this is why you <a href="http://spdeveloper.net/2009/12/programmatically-check-the-web-part-state/">check what state</a> the part is in before rendering contents for it.</p>
<p>You must also take into consideration the following use-cases that your web part will participate in:</p>
<ul>
<li>From the site’s <em>Web Part Gallery</em>, clicking the web part and rendering it in “<strong>Preview mode</strong>”.</li>
<li>When editing the page that has the web part in <strong>SharePoint Designer 2010</strong>, that the web part renders properly as a user-control.</li>
</ul>
<p>I want to focus on properly rendering the Web part in SharePoint Designer because this is a use-case that can be commonly missed. Today, I was testing a web part I am developing for Zenpo Software Innovations when I encountered the following error in SharePoint Designer 2010:</p>
<div class="wp-caption aligncenter" style="width: 661px"><a href="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Error-Creating-Control.png"><img title="SPD-Error-Creating-Control" src="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Error-Creating-Control.png" alt="Error Creating Control in SharePoint Designer" width="651" height="422" /></a><p class="wp-caption-text">“Error Creating Control” – what does that mean?</p></div>
<p style="text-align: left;">&nbsp;</p>
<p style="text-align: left;">The web part’s <strong>OnLoad </strong>and <strong>OnInit </strong>event-handlers didn’t check that the page is in “<strong>design mode</strong>”. So, it tried to render its content as if it was in browse-mode.</p>
<p style="text-align: left;">The web part uses custom JavaScript. I have to register the scripts in order for the web part to use them. I implement the <a href="http://msdn.microsoft.com/en-us/library/xxc4wkxx.aspx" target="_blank">script-registration</a> during the OnInit event of the web part like the following:</p>
<pre class="brush: csharp;">
        protected override void OnInit(EventArgs e)
        {
                if (!Page.ClientScript.IsClientScriptIncludeRegistered(&quot;Script1&quot;))
                {
                     scriptRegistered = false;
                     Page.ClientScript.RegisterClientScriptInclude(&quot;Script1&quot;,
                            Page.ClientScript.GetWebResourceUrl(this.GetType(),
                            &quot;Zenposoft.Script1.js&quot;));
                }
        }
</pre>
<p>This script registration code works fine on the web browser. The problem is when this code is invoked during SPD-editing of the web part page, <strong>Page.ClientScript.GetWebResourceUrl</strong> returns empty string (under normal browsing state, it would have returned the URL to the WebResource.axd file). And you cannot pass an empty string URL to the Page.ClientScript.RegisterClientScriptInclude method (it will throw an exception if you pass an empty URL). The web part really had no business trying to register scripts during design-time.</p>
<p>You might say to yourself, “this shouldn’t be an issue since Web part pages are edited through the browser anyway”. Think again! Web designers at your organization will use SharePoint Designer at some point to edit SharePoint pages and if your web part cannot handle “design mode” correctly, the designers will see the ugly “<strong>Error Creating Control</strong>” in SPD. You cannot instill confidence in the use of your web part if it’s throwing exceptions in SharePoint Designer!!!</p>
<p>So, how do we fix this? You can use the one of the following or both:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.isdesigntime.aspx" target="_blank">SPContext.Current.IsDesignTime</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.control.designmode.aspx" target="_blank">Control.DesignMode</a></li>
</ul>
<p>I found this <a href="http://blogs.msdn.com/b/sharepointdesigner/archive/2008/10/03/best-practice-to-create-designer-friendly-server-controls.aspx" target="_blank">Microsoft SharePoint Designer Team Blog post</a> that has nice explanation on how to create “designer friendly controls”. <strong>SPContext.Current.IsDesignTime</strong> and <strong>Control.DesignMode</strong> are mentioned in that blog post.</p>
<p>First, I modify the OnInit event-handler such that I register scripts only when “not in design mode”:</p>
<pre class="brush: csharp;">
        protected override void OnInit(EventArgs e)
        {
            if (!this.DesignMode &amp;&amp; !SPContext.Current.IsDesignTime)
            {
                if (!Page.ClientScript.IsClientScriptIncludeRegistered(&quot;Script1&quot;))
                {
                    scriptRegistered = false;
                    Page.ClientScript.RegisterClientScriptInclude(&quot;Script1&quot;,
                            Page.ClientScript.GetWebResourceUrl(this.GetType(),
                            &quot;Zenposoft.Script1.js&quot;));
                }
            }
        }
</pre>
<p>After deploying the modified Web part, we check SharePoint Designer and the errors are gone:</p>
<div id="attachment_486" class="wp-caption aligncenter" style="width: 671px"><a href="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Error-Gone.png"><img class="size-full wp-image-486" title="SPD-Error-Gone" src="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Error-Gone.png" alt="SPD-Error-Gone" width="661" height="408" /></a><p class="wp-caption-text">SharePoint Designer Errors - Gone</p></div>
<p>Yes, the errors are gone but we need to display something where the web part(s) are embedded. If the web part in on the page, the designer should render something there; it can’t be just white-space.</p>
<p>In order to render something in “design mode”, we override the <strong>Render</strong> method of the web part:</p>
<pre class="brush: csharp;">
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode || SPContext.Current.IsDesignTime)
            {
                writer.Write(&quot;&lt;input type=&quot;button&quot; value=&quot;Web part is in edit mode...&quot; /&gt;&quot;);
            }
            else
            {
                base.Render(writer);
            }
        }
</pre>
<p>If the web part is in “design mode”, we render a simple HTML button. Otherwise, we let the base class (WebPart) handle the rendering. Deploy the updated web part and you should now see the following in SharePoint Designer:</p>
<div id="attachment_487" class="wp-caption aligncenter" style="width: 448px"><a href="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Render-Control.png"><img class="size-full wp-image-487" title="SPD-Render-Control" src="http://spdeveloper.net/wp-content/uploads/2011/08/SPD-Render-Control.png" alt="SPD-Render-Control" width="438" height="261" /></a><p class="wp-caption-text">Render HTML button in Desing Mode</p></div>
<p>To summarize, you will want to render web parts correctly in designers such as SPD 2010.  The likelihood that Web designers will edit SharePoint pages in SPD is high enough that you will want to “design mode” for your web part to run properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://spdeveloper.net/2011/08/web-part-error-creating-control-spd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create &#8216;Open Tool Pane&#8217; link in custom .NET Web Parts for SharePoint 2010</title>
		<link>http://spdeveloper.net/2011/07/create-open-tool-pane-link-in-custom-net-web-parts-for-sharepoint-2010/</link>
		<comments>http://spdeveloper.net/2011/07/create-open-tool-pane-link-in-custom-net-web-parts-for-sharepoint-2010/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 18:30:29 +0000</pubDate>
		<dc:creator>Gabe Hilado</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[Web Parts]]></category>

		<guid isPermaLink="false">http://spdeveloper.net/?p=466</guid>
		<description><![CDATA[Back in SharePoint 2007, it was pretty popular to use the built-in Javascript function MSOTlPn_ShowToolPane2Wrapper to put the Web part page into edit mode and have the Web part properties editable. Typically, you would put something like the following in your Web part class code: protected override void OnPreRender(EventArgs e) { if (this.Page != null) [...]]]></description>
			<content:encoded><![CDATA[<p>Back in SharePoint 2007, it was pretty popular to use the built-in Javascript function <strong>MSOTlPn_ShowToolPane2Wrapper</strong> to put the Web part page into edit mode and have the Web part properties editable. Typically, you would put something like the following in your Web part class code:</p>
<pre class="brush: csharp;">
protected override void OnPreRender(EventArgs e)
{
   if (this.Page != null)
   {
      if (string.IsNullOrEmpty(SelectedListName))
      {
            placeHolderLiteral.Text = &quot;
Web part is not configured. Please &lt;a href=&quot;\&amp;quot;javascript:MSOTlPn_ShowToolPane2Wrapper('Edit',&quot;&gt;edit the Web part&lt;/a&gt; and choose a list or library.

&quot;;
      }
   }
}
</pre>
<p>In the example above, I render a &#8220;please configure the Web part&#8221; message whenever the SelectedListName of the Web part is not set.</p>
<p>I&#8217;m currently developing a Web part for SharePoint 2010 and I wanted to have a &#8220;configure&#8221; link displayed for the Web part whenever Web properties (i.e. list name, view ID etc.) are not set or specified . I embed a code similar to the above snippet where I use <strong>MSOTlPn_ShowToolPane2Wrapper</strong><strong>. </strong>It didn&#8217;t work!</p>
<p>So, thinking like a developer, I observe how the out-of-the-box Web parts render an &#8220;open tool pane&#8221; hyperlink. I add an Image Viewer web part to a web part page and look at the structure of the hyperlink. Here&#8217;s what I saw:<br />
<a href="http://spdeveloper.net/wp-content/uploads/2011/07/ShowToolPane2Wrapper.png"></a></p>
<p><img class="aligncenter size-full wp-image-467" title="ShowToolPane2Wrapper function" src="http://spdeveloper.net/wp-content/uploads/2011/07/ShowToolPane2Wrapper.png" alt="ShowToolPane2Wrapper function" width="745" height="259" /><br />
I replaced my output code in my web part class from <strong>MSOTlPn_ShowToolPane2Wrapper to <strong>ShowToolPane2Wrapper </strong></strong>and it works!<br />
Please note that I encourage that you put &#8216;this&#8217; (object) as the second argument instead of &#8217;129&#8242;, or &#8217;16&#8242; as you see in some blogs out there. The second parameter is supposed to indicate which Web part to edit. Do you really know at run-time that your Web part is &#8217;129&#8242; or &#8217;16&#8242;? You don&#8217;t! So simply use &#8216;this&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://spdeveloper.net/2011/07/create-open-tool-pane-link-in-custom-net-web-parts-for-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regex Validation on SharePoint Forms</title>
		<link>http://spdeveloper.net/2009/11/regex-validation-on-sharepoint-forms/</link>
		<comments>http://spdeveloper.net/2009/11/regex-validation-on-sharepoint-forms/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 20:22:22 +0000</pubDate>
		<dc:creator>Gabe Hilado</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Data Validation]]></category>
		<category><![CDATA[Email Address]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://spdeveloper.net/?p=131</guid>
		<description><![CDATA[In my previous post, I talked about input-masking to help ensure the quality of the inputs from the user. Information such as phone numbers and ZIP code can be masked because format is anticipated well in advanced. There are certain types of inputs though that are tough or impossible to mask. Take the email address [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://spdeveloper.net/2009/10/input-masking-in-sharepoint-forms/">previous post</a>, I talked about input-masking to help ensure the quality of the inputs from the user. Information such as phone numbers and ZIP code can be masked because format is anticipated well in advanced. There are certain types of inputs though that are tough or impossible to mask. Take the email address for example: masking is not applicable (or cumbersome) to implement because there are so many formats that you can apply to an email address, the lengths are variable, and some special characters can be inserted anywhere before the @ sign.</p>
<p>Some classes of data are best validated using <strong>regular expressions</strong> (or <strong>regex</strong>). Email address is typically validated using regex. Another field or data-element similar to email address in the way you validate it is the URL (Web sites, etc.)</p>
<p>The OOTB SharePoint forms for the lists (NewForm.aspx and EditForm.aspx) will validate many things. For example, you can make a field be a required field and the OOTB forms will make sure the user input something. Also, when you specify a SharePoint column to be of type &#8220;something other than text&#8221; (types such &#8220;Numbers&#8221; and &#8220;Dates&#8221;) SharePoint will enforce validations on those fields as well. What&#8217;s the problem with email addresses and URLs in OOTB SP forms? They are treated as text and the values&#8217; correctness are not enforced. Here&#8217;s an example:</p>
<div id="attachment_132" class="wp-caption aligncenter" style="width: 512px"><img class="size-full wp-image-132  " title="An invalid email address allowed to be saved in SharePoint" src="http://spdeveloper.net/wp-content/uploads/2009/11/invalid_email.jpg" alt="Invalid email address allowed to be saved by SharePoint" width="502" height="470" /><p class="wp-caption-text">An invalid email address allowed to be saved in SharePoint</p></div>
<div class="mceTemp mceIEcenter"> </div>
<div class="mceTemp mceIEcenter" style="text-align: left;">
<p>You can type in &#8220;<em>asdf</em>&#8221; on the email address field and it will be okay for the form. That&#8217;s not right! So let&#8217;s &#8220;tweak&#8221; the SharePoint forms to make them validate email address.</p>
<p>For this solution, we again use jQuery JavaScript library like the input-masking exercise. We will also need the <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">validation plugin</a>. Upload the validation plug-in into a &#8220;scripts&#8221; library in your SharePoint site.</p>
<p>Next, open-up the SharePoint Site and edit the list&#8217;s NewForm.aspx or EditForm.aspx in SharePoint Designer. In this example, we are editing the Contacts List EditForm.aspx. You want to look for the table element whose ID is <em>onetIDListForm</em>. Insert the following code just below the table.onetIDListForm element (notice that we&#8217;re simply extending the source code from the <a href="http://spdeveloper.net/2009/10/input-masking-in-sharepoint-forms/">previous post</a>):</p>
<p> </p>
<pre class="brush: jscript;">
&lt;script src=&quot;../../scripts/jquery-1.3.2.js&quot;&gt;&lt;/script&gt;

 

&lt;!-- jquery plugin for input-masking... --&gt;
&lt;script src=&quot;../../scripts/jquery.maskedinput-1.2.2.min.js&quot;&gt;&lt;/script&gt;

 

&lt;!-- jquery plugin for validation... --&gt;
&lt;script src=&quot;../../scripts/jquery.validate.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
 $(document).ready(function() {
 
  // Apply input-masks to &quot;phone number&quot; fields and ZIP
  $(&quot;input[title='Business Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
  $(&quot;input[title='Home Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
  $(&quot;input[title='Mobile Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
  $(&quot;input[title='ZIP/Postal Code']&quot;).mask(&quot;99999&quot;);

    
  /*--- Lines from this point onwards: validation of the email address field ---*/

  // Apply the &quot;validation plugin&quot;
  $(&quot;#aspnetForm&quot;).validate({ 
   submitHandler: function(form) {

   }
  });
  
  // Since it's hard to match input/field by ID value, use the validation plugin's &quot;rules()&quot; method
  // Here, we apply the &quot;email rule&quot; to the input whose title is &quot;E-mail Address&quot;
  $(&quot;input[title='E-mail Address']&quot;).rules(
    &quot;add&quot;,
    { email: true,
      messages: {
       email: &quot;Please enter a valid email address.&quot;
      }
    }
  );
  
  // We add jquery's blur() event to the email-address field so that the form
  // will keep the focus on the field until the user has corrected the problem
  $(&quot;input[title='E-mail Address']&quot;).blur(function() {
   if (!$(&quot;input[title='E-mail Address']&quot;).valid()) {
    this.focus();
   }
  });
  
  // Finally, prevent the user from clicking OK if the email address is invalid
  $(&quot;input[value='OK']&quot;).focus(function(){
   if (!$(&quot;input[title='E-mail Address']&quot;).valid()) {
    alert(&quot;Please enter a valid email address.&quot;);
    $(&quot;input[title='E-mail Address']&quot;).focus();
   }   
  });

 });
&lt;/script&gt;
</pre>
<p>After the code snippet is inserted into the form, the SharePoint Contact form will now ensure that the inputs conform to the email address field:</p>
<div id="attachment_133" class="wp-caption aligncenter" style="width: 515px"><img class="size-full wp-image-133 " title="Email address now validated" src="http://spdeveloper.net/wp-content/uploads/2009/11/validating_email.jpg" alt="Email address now validated" width="505" height="372" /><p class="wp-caption-text">Email address now validated</p></div>
</div>
<div class="mceTemp mceIEcenter" style="text-align: left;"> </div>
]]></content:encoded>
			<wfw:commentRss>http://spdeveloper.net/2009/11/regex-validation-on-sharepoint-forms/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Input-Masking in SharePoint Forms</title>
		<link>http://spdeveloper.net/2009/10/input-masking-in-sharepoint-forms/</link>
		<comments>http://spdeveloper.net/2009/10/input-masking-in-sharepoint-forms/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 21:10:34 +0000</pubDate>
		<dc:creator>Gabe Hilado</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Data Validation]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Input Masks]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://spdeveloper.net/?p=102</guid>
		<description><![CDATA[It&#8217;s that time of the year again when kids will be kids and grown-ups can be kids. I&#8217;m talking about Halloween. Halloween is one of the more festive events in the year, in my opinion. You have tons of candies, parties everywhere, and costumes galore. When it comes to costumes, there are some people who [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s that time of the year again when kids will be kids and grown-ups can be kids. I&#8217;m talking about Halloween. Halloween is one of the more festive events in the year, in my opinion. You have tons of candies, parties everywhere, and costumes galore. When it comes to costumes, there are some people who really dress-up.  And then there some who simply don a mask for their Halloween costume. <strong>Masking</strong> is the inspiration for today&#8217;s post: input-masking in SharePoint Forms.</p>
<p>Input-masking is a programmng technique which allows applications specify the rules for the data-inputs. For example, ZIP codes should be 5-digit numeric number or phone numbers should be numeric (mostly) and appear in the form of <em>(111) 555-0000</em>. In SharePoint, you will see fields in some forms that should have a mask for the input. Take the Contacts List as an example: a contact has phone numbers and ZIP code. Yet, SharePoint doesn&#8217;t validate what users input in those fields. Take a look:</p>
<div id="attachment_112" class="wp-caption aligncenter" style="width: 507px"><img class="size-full wp-image-112" title="InvalidPhoneNumber" src="http://spdeveloper.net/wp-content/uploads/2009/10/InvalidPhoneNumber2.jpg" alt="Invalid format for phone-number in SP Contact Form" width="497" height="466" /><p class="wp-caption-text">Invalid format for phone-number in SP Contact Form</p></div>
<div id="attachment_116" class="wp-caption aligncenter" style="width: 474px"><img class="size-full wp-image-116" title="SPAllowedInvalidPhone" src="http://spdeveloper.net/wp-content/uploads/2009/10/SPAllowedInvalidPhone1.jpg" alt="SharePoint allowed invalid phone number" width="464" height="286" /><p class="wp-caption-text">SharePoint allowed invalid phone number</p></div>
<p>If SharePoint site-definition forms will not enforce input-masking, we can easily apply it ourselves using jQuery JavaScript library. You can dowload the jQuery jlibrary <a href="http://docs.jquery.com/Downloading_jQuery">here</a>. (You can link to jQuery library from one of the Google sites; I only recommend you download the js file itself in the event you&#8217;re running your development sandbox in an isolated network). You will also need to <a href="http://digitalbush.com/projects/masked-input-plugin/">download the input-mask  jQuery plugin</a>. Upload the jQuery js file and the input-mask plug-in (another js file) into a &#8220;scripts&#8221; library in your SharePoint site.</p>
<p>Open-up the SharePoint Site and edit the list&#8217;s NewForm.aspx or EditForm.aspx in SharePoint Designer. In this example, we are editing the Contacts List EditForm.aspx. You want to look for the table element whose ID is <em>onetIDListForm</em>. Insert the following code just below the table.onetIDListForm element:</p>
<pre class="brush: xml;">
&lt;script src=&quot;../../scripts/jquery-1.3.2.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../../scripts/jquery.maskedinput-1.2.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
 $(document).ready(function() {
 $(&quot;input[title='Business Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
 $(&quot;input[title='Home Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
 $(&quot;input[title='Mobile Phone']&quot;).mask(&quot;(999) 999-9999&quot;);
 $(&quot;input[title='ZIP/Postal Code']&quot;).mask(&quot;99999&quot;);
 });
&lt;/script&gt;
</pre>
<p>I will write another blog-post about jQuery. For now, I will quickly explain what the code above does. <em>$(&#8220;input[title='Business Phone']&#8220;) </em>selects the text-box for the Business Phone field. There are<a href="http://docs.jquery.com/Selectors"> different ways to select elements using jQuery</a>. What I did above as select the input whose title is &#8220;Business Phone&#8221;. After the element has been matched, we call the plugin&#8217;s <em><strong>Mask</strong>()</em> method. Why not select the input-element using the ID? Because you do not know nor control what ID value will be generated by SharePoint. The typical input element generated by SharePoint looks like the following:</p>
<pre class="brush: xml;">
&lt;input name=&quot;ctl00$m$g_d1b9fb52_9f94_4f65_b370_2c23efb30c7e$
             ctl00$ctl04$ctl08$ctl00$ctl00
             $ctl04$ctl00$ctl00$TextField&quot;
       type=&quot;text&quot; maxlength=&quot;255&quot;
       id=&quot;ctl00_m_g_d1b9fb52_9f94_4f65_b370_2c23efb30c7e_ctl00
             _ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_TextField&quot;
       title=&quot;Mobile Phone&quot; /&gt;
</pre>
<p>Selecting the input-text using the title attribute is easier and will be consistently rendered by SharePoint that way anyway (assuming the list column stays the same and doesn&#8217;t get modified.)</p>
<p>After the masks have been applied, the SharePoint Contact form will now ensure that the inputs conform to the phone number and ZIP code format:</p>
<p><em> </em></p>
<div id="attachment_118" class="wp-caption alignnone" style="width: 488px"><em><em><img class="size-full wp-image-118" title="MasksApplied" src="http://spdeveloper.net/wp-content/uploads/2009/10/MasksApplied.jpg" alt="Input-masks in a SharePoint Contact Form" width="478" height="369" /></em></em><p class="wp-caption-text">Input-masks in a SharePoint Contact Form</p></div>
<p><em> </em><br />
I haven&#8217;t had a chance to blog about jQuery yet but I will do this sometime next week. jQuery is a very useful and powerful JavaScript library. There are many other tricks you can do in SharePoint with jQuery and I will discus them next week.</p>
<p>I hope you guys enjoy Halloween tomorrow. And don&#8217;t eat too many candies!</p>
]]></content:encoded>
			<wfw:commentRss>http://spdeveloper.net/2009/10/input-masking-in-sharepoint-forms/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

