In SharePoint 2013 there are cases when you would like to
validated client side URL and prompt user if it is invalid URL. Below code
snippet can be used for this validation. Edit newform.aspx and editform.aspx,
place a script editor webpart with following code snippet. You can
alternatively created feature and on feature activation event add script edit,
inject this js code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> | |
<script> | |
function isValid(val) { | |
var re = /^(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/; | |
return re.test(val); | |
} | |
function PreSaveAction(){ | |
var commentsBox = findFieldControl('URL Required Field'); //find field control by title | |
if (!isValid(commentsBox.val())) { | |
var errorHtml = '<span class="ms-formvalidation" title="errMessage"><br/><span role="alert">Invalid URL, please rectify URL and try again.<br></span></span>'; | |
commentsBox.after(errorHtml); | |
return false; | |
} | |
return true; | |
} | |
function findFieldControl(fieldTitle) | |
{ | |
var control = $('[title="' + fieldTitle + '"]'); | |
var errMessage= $('span[title="errMessage"]'); | |
if(errMessage.length>0) | |
{ | |
errMessage.remove(); | |
} | |
return control; | |
} | |
</script> |