{"id":3681,"date":"2012-04-13T14:30:41","date_gmt":"2012-04-13T06:30:41","guid":{"rendered":"http:\/\/www.sharepointboost.com\/blog\/?p=3681"},"modified":"2023-07-31T11:39:56","modified_gmt":"2023-07-31T03:39:56","slug":"improving-your-sharepoint-user-experience-with-jquery-client-side-form-validation","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/improving-your-sharepoint-user-experience-with-jquery-client-side-form-validation\/","title":{"rendered":"Improving Your SharePoint User Experience with jQuery Client Side Form Validation"},"content":{"rendered":"<p>SharePoint forms for the lists NewForm.aspx and EditForm.aspx can validate a variety of things. For example, you can make a field be a required field and the forms will force the user to input something. Additionally, you also can specify that a SharePoint column be of type &#8220;<strong>something other than text<\/strong>&#8221; (types such &#8220;Numbers&#8221; and &#8220;Dates&#8221;) and SharePoint will enforce validations on those fields as well.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Problem<\/span><\/strong>:<\/p>\n<p>However, you can not specify a SharePoint column to be of type &#8220;<strong>email addresses<\/strong>&#8221; or &#8220;<strong>phone number<\/strong>&#8221; .They are just treated as text and the values&#8217;validity are not enforced.<\/p>\n<p><span style=\"text-decoration: underline;\">Here is an example<\/span>:<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic1-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3682\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic1-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" alt=\"\" width=\"437\" height=\"289\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic1-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png 437w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic1-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate-300x198.png 300w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Note<\/span><\/strong>: You cannot validate a field&#8217;s value on the client-side, which means that when click &#8220;<strong>OK<\/strong>&#8220;, you need to refresh the entire page before you can determine there are input errors.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Solution<\/span><\/strong>:<\/p>\n<p>This solution will enable you validate forms on <strong>client side<\/strong> and in <strong>real time<\/strong>.<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic2-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3684\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic2-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" alt=\"\" width=\"576\" height=\"532\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic2-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png 576w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic2-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1-300x277.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic2-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><\/a>1. First we need jQuery JavaScript library and the jQuery Validation Plugin.<\/p>\n<p>2.Upload the validation plug-in into a &#8220;jquery&#8221; library in your SharePoint site.<\/p>\n<address><em>JQuery Validation Plugin is written and maintained by Jorn Zaefferer, a member of the <\/em><a href=\"http:\/\/jquery.org\/team\" target=\"_blank\" rel=\"noopener noreferrer\"><em>jQuery team<\/em><\/a><em>, lead developer on the jQuery UI team and maintainer of QUnit. It was started back in the early days of jQuery in 2006, and updated and improved since then.<\/em><\/address>\n<p><em> <\/em>3. Next, open the SharePoint Site and edit the list&#8217;s <strong>NewForm.aspx<\/strong> or <strong>EditForm.aspx<\/strong> in SharePoint Designer.<\/p>\n<p>4. In this example, we are editing the Contacts List EditForm.aspx.<\/p>\n<p>a. Click &#8220;<strong>All Files_<\/strong><strong>Lists<\/strong>,&#8221; and find the contact list.<\/p>\n<p>b. Right-click the <strong>NewForm.aspx<\/strong>, and choose &#8220;<strong>E<\/strong><strong>dit <\/strong><strong>F<\/strong><strong>ile<\/strong><strong> <\/strong><strong>I<\/strong><strong>n <\/strong><strong>A<\/strong><strong>dvanced <\/strong><strong>M<\/strong><strong>ode<\/strong>.&#8221;<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic3-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3690\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic3-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" alt=\"\" width=\"364\" height=\"244\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic3-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png 364w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic3-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1-300x201.png 300w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic3-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><\/a>c. Add links to the jQuery script and the plugin with following code:<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic4-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3691\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic4-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" alt=\"\" width=\"583\" height=\"86\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic4-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png 583w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic4-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate-300x44.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic4-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><\/a>5. So once you have all the scripts you need on the page, we just need to attach the behavior to the form field. To do this we can add some JavaScript to the page in a Content Editor web part.<\/p>\n<p>6. Return to SharePoint. SharePoint will launch the list forms in a dialog by default. It is not convenient to edit pages.<\/p>\n<p>7. Close Dialog Mode by modifying <strong>List Setting-&gt; Advanced Setting-&gt; Dialogs.<\/strong><\/p>\n<p><strong><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic5-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3692\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic5-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" alt=\"\" width=\"489\" height=\"117\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic5-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png 489w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic5-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate-300x71.png 300w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/a><\/strong><\/p>\n<p><strong><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic5-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><\/a><\/strong>8. Now, open our list, and click &#8220;<strong>Add new item<\/strong>.&#8221;<\/p>\n<p>9. Open <strong>NewForm.aspx<\/strong>.<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic6-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3693\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic6-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" alt=\"\" width=\"489\" height=\"292\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic6-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png 489w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic6-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate-300x179.png 300w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Pic6-Improving-SharePoint-User-Experience-With-JQuery-Client-Side-Form-Validate.png\" target=\"_blank\" rel=\"noopener noreferrer\"><\/a>10. Select the <strong>Site Actions<\/strong><strong> <\/strong><strong>-&gt; Edit<\/strong><strong> <\/strong><strong>Page<\/strong>, enter edit mode.<\/p>\n<p>11. Insert an HTML Form Web Part.<\/p>\n<p>12. Open <strong>WebPart<\/strong><strong> <\/strong><strong>Properties<\/strong><strong> <\/strong><strong>-&gt;<\/strong><strong> <\/strong><strong>Source<\/strong><strong> <\/strong><strong>Editor<\/strong>.<\/p>\n<p>13. Delete the default HTML content.<\/p>\n<pre>       $(document).ready(function () {\r\n           var validator = $(\"form\").validate({ errorClass: \"ms-formvalidation\" });\r\n           $(\"input[title='E-mail Address']\").rules(\"add\", { email: true });\r\n           $(\"input[title='Last Name']\").rules(\"add\", { required: true });\r\n           $(\"input[title='Business Phone']\").rules(\"add\", { phoneUS: true });\r\n           $(\"input[title='Home Phone']\").rules(\"add\", { phoneUS: true });\r\n           $(\"input[title='Mobile Phone']\").rules(\"add\", { phoneUS: true });\r\n           $(\"input[value='Save']\").each(function () {\r\n               var js = $(this).attr(\"onclick\");\r\n               $(this).attr(\"onclick\", \"\");\/\/remove sharepoint click handler...\r\n               $(this).click(function (event) {\r\n                   if (!validator.form()) {\r\n                       return false;\r\n                   } else {\r\n                       \/\/call sharepoint click handler..\r\n                       eval(\" ( function () {\" + js + \" })();\");\r\n                   }\r\n               })\r\n           })\r\n       });<\/pre>\n<p><strong><span style=\"text-decoration: underline;\">Other Useful links<\/span><\/strong>:<\/p>\n<p><a href=\"http:\/\/davecavins.com\/2010\/02\/improving-sharepoint-forms-hints\/\" target=\"_blank\" rel=\"noopener noreferrer\">Improving #SharePoint Forms-Hints<\/a><\/p>\n<p><a href=\"http:\/\/davecavins.com\/2010\/05\/improving-sharepoint-forms-%E2%80%93-watermarks\/\" target=\"_blank\" rel=\"noopener noreferrer\">Improving #SharePoint Forms-Watermarks<\/a><\/p>\n<p><a href=\"http:\/\/davecavins.com\/2010\/02\/character-count-on-sharepoint-fields\/\" target=\"_blank\" rel=\"noopener noreferrer\">Improving #SharePoint Forms-Character Count<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint forms for the lists NewForm.aspx and EditForm.aspx can validate a variety of things. For example, you can make a field be a required field and the forms will force the user to input something. Additionally, you also can specify that a SharePoint column be of type &#8220;something other than text&#8221; (types such &#8220;Numbers&#8221; and [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[125],"tags":[339,337,338],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3681"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3681"}],"version-history":[{"count":15,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"predecessor-version":[{"id":9244,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3681\/revisions\/9244"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}