2

I want to validate a few fields of my customnewform.aspx while saving data in SharePoint 2010 using SPServices.

Instead of using the existing list validation settings, how to achieve mandatory fields, dates, etc using SPServices & jQuery & JavaScript?

When Presaveaction is called, can we access the field values using SPServices & jQuery?

3

In PreSaveAction, you would just use jquery to get the field value and do your validation, basic example.

function PreSaveAction() {
    var title= $("input[Title='Title']").val();
    if(!title.length){
     alert("oops, missed a field!");
     return false;
    }
    else {
     return true;
    }
}

You could use SPServices functions too if you needed to do other things, like update relational lists or anything like that, or use any of the utility functions. In the case of SharePoint Online, you might be running into this issue: http://sympmarc.com/2014/01/23/office-365-update-changes-display-name-on-required-fields/

  • Eric, I already tried your code,but js gave me an error " undefined".That's why I asked this & trying to use SPServices approach. Let me try once more & update here. – SaMolPP Oct 8 '15 at 14:10
  • If it throws undefined, then the selector is probably not right, I whipped this up from memory so it might not be 100% accurate. You need to find your fields and test what validation you require, if it fails return false to cancel the save. – Eric Alexander Oct 8 '15 at 14:14
2

To Read a SP List items using SPServices

Include below js files in .aspx page

<script type="text/javascript" src="/_layouts/DemoGetListItemSPServices/script/jquery-1.7.2.js"></script>
<script type="text/javascript" src="/_layouts/DemoGetListItemSPServices/script/jquery.SPServices-0.7.2.js"></script>

Sample js code.

<div><ul id="taskItems"></ul></div>>

And add this script code block below to use SPServices operation:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
  var myQuery = 
       "<Query>" +
          "<Where>" +
             "<Eq>" +
               "<FieldRef Name='PercentComplete' /><Value Type='Number' >1</Value>" +
             "</Eq>" +
          "</Where>" +
       "</Query>";
  $().SPServices({
    webUrl: "https://itsharedspace/"
    operation: "GetListItems",
    async: false,
    listName: "Tasks",
    CAMLQuery: myQuery,
    CAMLRowLimit: 100,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
        //<em>complete function</em>
    }
  });
});
</script>

Reference : Click Here

  • Thanks.but how can I access the field values and how can I compare from the above code? If I am not wrong its called when the Page is loaded,and my validation has to occur when the Save button is clicked and PreSaveAction is called – SaMolPP Oct 8 '15 at 13:33
  • You may write those stuff in one function and call that function when the submit button hit. use OnClick event or use jQuery function to call the validation function when button click. – Thirusanguraja Venkatesan Oct 8 '15 at 14:00
  • Write that code of SPServices into the presaveaction function. You can write the code there and do all the needed validation in PreSaveAction function using SPServices. – Niranjan Kulkarni Oct 8 '15 at 15:20
  • i want to add a spclient side people picker control in my newform.aspx and wann insert into the person or group field in my splist. – SaMolPP Oct 10 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.