SharePoint – InfoPath: Validate attachment URL for special characters

I did a ton of searching for this and could only find 1 solution. With this solution, if you have special characters in your file name and hit submit a warning pops up and tells you that  you have special characters in your file name. It will not let you submit the form until that is fixed.

The solution is as follows:

Create an InfoPath form the way you usually would.

In SharePoint, place an InfoPath form web part into a web part page.

Add a Content Editor web part and place the following JavaScript inside the html part of it.

You will need to replace the bolded id below with your own. That text is the id for your submit button. In most browsers you can right click on the submit button and click inspect element to find it. It is will read something like this: <input style=”text-align:center” onclick=”return (Button.OnClick(this, event));” onfocus=”return (Button.OnFocus(this, event));” id=”ctl…

———————————————————————————————————————————————————————————————————-

<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“input[id=’ctl00_m_g_ed36b5b8_a7c8_45d8_a19a_ac7017df0aff_FormControl0_V1_I1_B3‘]”).live(“mousedown”, function()
{

spans = $(‘li[scriptclass=”SharePointFileAttachmentContainer”]’).find(‘span[id^=”ctl00_m”]’).find(‘span’);
spans.each(function(){
// alert( $(this).text() );
specialcharecter($(this).text());

});
function specialcharecter(FileName)
{
var iChars = “!`@#$%^&*()+=’;,/{}|\”:<>?~”;
// var data = document.getElementById(“txtCallSign”).value;
var data = FileName;

for (var i = 0; i < data.length; i++)
{
if (iChars.indexOf(data.charAt(i)) != -1)
{
alert (“Attached file has special characters (ex. & # $ %). \nThese are not allowed.“);
// document.getElementById(“txtCallSign”).value = “”;
return false;
}
}
}

});
});
</script>​​​​

———————————————————————————————————————————————————————————————————-

The bolded area above next to “alert” is the text that will display upon submit if there are special characters in the file name.

Here is the link to the answered post in which I found the solution:
http://social.msdn.microsoft.com/Forums/sharepoint/pt-BR/c9ebf3fc-062c-4ad6-9d0d-fde440e0edff/sharepoint-info-path-form-attachment-file-name-should-not-contain-special-character-

This entry was posted in InfoPath, SharePoint and tagged , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *