Posted by: Gabe Hilado in jQuery,SharePoint on November 10th, 2009

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 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.

Some classes of data are best validated using regular expressions (or regex). 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.)

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 “something other than text” (types such “Numbers” and “Dates”) SharePoint will enforce validations on those fields as well. What’s the problem with email addresses and URLs in OOTB SP forms? They are treated as text and the values’ correctness are not enforced. Here’s an example:

Invalid email address allowed to be saved by SharePoint

An invalid email address allowed to be saved in SharePoint

 

You can type in “asdf” on the email address field and it will be okay for the form. That’s not right! So let’s “tweak” the SharePoint forms to make them validate email address.

For this solution, we again use jQuery JavaScript library like the input-masking exercise. We will also need the validation plugin. Upload the validation plug-in into a “scripts” library in your SharePoint site.

Next, open-up the SharePoint Site and edit the list’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 onetIDListForm. Insert the following code just below the table.onetIDListForm element (notice that we’re simply extending the source code from the previous post):

 

<script src="../../scripts/jquery-1.3.2.js"></script>

 

<!-- jquery plugin for input-masking... -->
<script src="../../scripts/jquery.maskedinput-1.2.2.min.js"></script>

 

<!-- jquery plugin for validation... -->
<script src="../../scripts/jquery.validate.min.js"></script>

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

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

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

   }
  });
  
  // Since it's hard to match input/field by ID value, use the validation plugin's "rules()" method
  // Here, we apply the "email rule" to the input whose title is "E-mail Address"
  $("input[title='E-mail Address']").rules(
    "add",
    { email: true,
      messages: {
       email: "Please enter a valid email address."
      }
    }
  );
  
  // 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
  $("input[title='E-mail Address']").blur(function() {
   if (!$("input[title='E-mail Address']").valid()) {
    this.focus();
   }
  });
  
  // Finally, prevent the user from clicking OK if the email address is invalid
  $("input[value='OK']").focus(function(){
   if (!$("input[title='E-mail Address']").valid()) {
    alert("Please enter a valid email address.");
    $("input[title='E-mail Address']").focus();
   }   
  });

 });
</script>

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:

Email address now validated

Email address now validated

 
Posted by: Gabe Hilado in jQuery,SharePoint on October 30th, 2009

It’s that time of the year again when kids will be kids and grown-ups can be kids. I’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. Masking is the inspiration for today’s post: input-masking in SharePoint Forms.

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 (111) 555-0000. 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’t validate what users input in those fields. Take a look:

Invalid format for phone-number in SP Contact Form

Invalid format for phone-number in SP Contact Form

SharePoint allowed invalid phone number

SharePoint allowed invalid phone number

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 here. (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’re running your development sandbox in an isolated network). You will also need to download the input-mask  jQuery plugin. Upload the jQuery js file and the input-mask plug-in (another js file) into a “scripts” library in your SharePoint site.

Open-up the SharePoint Site and edit the list’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 onetIDListForm. Insert the following code just below the table.onetIDListForm element:

<script src="../../scripts/jquery-1.3.2.js"></script>
<script src="../../scripts/jquery.maskedinput-1.2.2.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
 $("input[title='Business Phone']").mask("(999) 999-9999");
 $("input[title='Home Phone']").mask("(999) 999-9999");
 $("input[title='Mobile Phone']").mask("(999) 999-9999");
 $("input[title='ZIP/Postal Code']").mask("99999");
 });
</script>

I will write another blog-post about jQuery. For now, I will quickly explain what the code above does. $(“input[title='Business Phone']“) selects the text-box for the Business Phone field. There are different ways to select elements using jQuery. What I did above as select the input whose title is “Business Phone”. After the element has been matched, we call the plugin’s Mask() 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:

<input name="ctl00$m$g_d1b9fb52_9f94_4f65_b370_2c23efb30c7e$
             ctl00$ctl04$ctl08$ctl00$ctl00
             $ctl04$ctl00$ctl00$TextField"
       type="text" maxlength="255"
       id="ctl00_m_g_d1b9fb52_9f94_4f65_b370_2c23efb30c7e_ctl00
             _ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_TextField"
       title="Mobile Phone" />

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’t get modified.)

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:

Input-masks in a SharePoint Contact Form

Input-masks in a SharePoint Contact Form


I haven’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.

I hope you guys enjoy Halloween tomorrow. And don’t eat too many candies!