Validate a SharePoint list column with Regular Expression

Validate a SharePoint list column with Regular Expression

Column validations is in SharePoint 2010 a feature for lists, this is great when you need to validate a column like compare if a number is between some interval or if an end date is later than a start date.

Regular expression is not available out of the box as a feature in SharePoint 2010 column validation so how can we do this? If it´s about a smaller implementation like just one list form then jQuery would be the alternative. What about the third-party market? Take a look at Bamboos product called Bamboo validate column. If you need regular expression as an inbuilt feature in all lists in you environment, this product is definitely something to looking into.

How to use regular expression for validation with jQuery then? Let´s first take a look at the jQuery plugin called jQuery.Validate and how we can use this in SharePoint. This plugin offers a lot of options for customizations. JQuery validate has been around since early 2006 and is written by Jörn Zaefferer, lead developer of the jQuery UI team and it.

In following steps we will link our friend the Content Editor Web Part (CEWP) to a jQuery script stored in a document library at the same site. This apporach could be done by refering the master page,  the page layout or as a packed web part if you prefer to.

01 <script type="text/javascript" src=""></script>
02 <script type="text/javascript" src=""></script>
04 <script type="text/javascript">
05 $(document).ready(function() {
07 $("#aspnetForm").validate({
08 submitHandler: function(form) {
09 }});
11 $.validator.addMethod(
12       "regex",
13       function(value, element, regexp) {
14           var check = false;
15           var re = new RegExp(regexp);
16           return this.optional(element) || re.test(value);
17       },""
18 );
21 $("input[title='Title']").rules("add",{
22 required: true,
23 regex:"^[A-Za-z0-9- _]{1,100}$",
24 messages: {
25 regex: "<br />One or more special charachters.."
26 }
27 });
29 // BLUR -  Set focus after alert
30 $("input[title='Title']").blur(function() {
31 if (!$("input[title='Title']").valid()) {
32 this.focus();
33 }
34 });
36 // SAVE - Cannot save if regex rules adds
37 $("input[value='Save']").focus(function(){
38 if (!$("input[title='Title']").valid()) {
39 alert("You cannot save this Item..");
40 $("input[title='Title']").focus();
41 }
42 });
44 });
45 </script>


Let’s start


About sharepointsriram

9+ Yrs of IT experience
This entry was posted in Sharepoint 2010 Server Object Model. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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