0
var j = jQuery.noConflict();

function PreSaveAction() { 
    var txtTitle = $("#ctl00_ctl41_g_648c8386_16ca_4729_9c59_c9757150bafe_ff21_ctl00_ctl00_TextField").val();    //Application Name
    //alert(txtTitle);
    if (txtTitle == '') {        
        alert("Value Cannot be Blank in Application Name");          
        return false;
    } else {
        return true;
    } 


    var txtTitle1 = $.("#ctl00_ctl41_g_648c8386_16ca_4729_9c59_c9757150bafe_ff31_ctl00_ctl00_TextField_inplacerte").val();    //Description
    if (txtTitle1 == '') {        
        alert("Value Cannot be Blank in Description");          
        return false;
    } else {
        return true;
    }    
}
5
  • 5
    You should not return true until the end of the method Jun 22, 2016 at 13:46
  • What is the error ?
    – user51831
    Jun 22, 2016 at 13:58
  • I suggest you to use this code $("input[title='your field name']").val() to get text value. Because text filed ID is dynamicly genereated id in sharepoint and return true at end of the method.
    – Sabitha S
    Jun 22, 2016 at 13:59
  • And try not to use . after $
    – user51831
    Jun 22, 2016 at 14:02
  • @Gol.D error is that presave is working well when I check for application name and not for description field
    – 1592boy
    Jun 22, 2016 at 14:16

3 Answers 3

1

You have two problems:

  1. Your txtTitle1 variable is equal to $.(. Just remove that dot after the dollar sign.
  2. Your Description field seems to contain rich HTML because the field ID in the DOM contains the word _inplacerte at the end, therefore the value() jQuery function will not work since the field is not an input or textarea element, but rather a DIV element. In this case you need to replace value() with text().trim().

Also, here a version of your code without jQuery. The $get function is a shortcut for Sys.UI.DomElement.getElementById and it is available everywhere on SharePoint as it is part of the ASP.NET Ajax library. There is no point in using jQuery, unless you use it for some other things on the page.

function PreSaveAction() { 
    var applicationNameField = $get("ctl00_ctl41_g_648c8386_16ca_4729_9c59_c9757150bafe_ff21_ctl00_ctl00_TextField");
    var descriptionField = $get("ctl00_ctl41_g_648c8386_16ca_4729_9c59_c9757150bafe_ff31_ctl00_ctl00_TextField_inplacerte");

    if (applicationNameField && applicationNameField.value.trim() === "") {        
        alert("Value Cannot be Blank in Application Name");          
        return false;
    }

    if (descriptionField && descriptionField.textContent.trim().length < 2) {        
        alert("Value Cannot be Blank in Description");          
        return false;
    }

    return true;
}
6
  • i am getting alert for application name but when i check for description it fails
    – 1592boy
    Jun 22, 2016 at 14:14
  • Ah, I know why. I have not noticed that your Description field's id contains the word "_inplacerte", which means the field contains rich HTML, not just plain text. I have updated my answer, so please try again. Jun 22, 2016 at 14:19
  • still not getting alert box message when description field is left blank
    – 1592boy
    Jun 22, 2016 at 14:24
  • Yeah, you're right, it should not work because the rich HTML multiline fields are a bit tricky. Even though the field is empty, the length of text is still equal to 1 or more if you have empty spaces. I have updated the answer, so please try again. Jun 22, 2016 at 15:21
  • Also, I should have mentioned that if you DO NOT need rich HTML in the description field, you can just change the type of text to be "Plain text" in field settings. Then doing descriptionField.value.trim() === "" would work without so much hassle. Jun 22, 2016 at 15:23
0

I suggest you to use this code $("input[title='your field name']").val() to get text value for best practice. Because text field ID is dynamically generated id in SHAREPOINT and return true at end of the method

your code should be

var j = jQuery.noConflict();
        function PreSaveAction() { 
        var txtTitle = $("input[title='your Application ield name']").val() ;    //Application Name

        var txtTitle1 = $("input[title='your descriptiom field name']").val() ; 

           //alert(txtTitle);
           if(txtTitle == '' && txtTitle1 == '')
      {        
           alert("Value Cannot be Blank in Application Name");          
           return false;
        }
        else{
           return true;
            }   

        }
1
  • Sabitha S, the field ID is dynamically generated, but once the field is created the ID does not change unless the field is deleted and created again; the name of the field can however easily change and thus break the code. Also, getting DOM elements by ID is more performant, although this does not really matter in this scenario. Jun 22, 2016 at 14:11
0

Since the field is a Rich text multi-line field, if you look into the DOM (using Firebug or IE tool) there would be some values in it. like empty < p> tags.

Try the following:

function stripHtml(body) {
  var regex = /(<([^>]+)>)/ig;
  var result = body.replace(regex, "");
  return result;
}

var textFieldHtml = $("div[id='ctl00_ctl41_g_648c8386_16ca_4729_9c59_c9757150bafe_ff31_ctl00_ctl00_TextField_inplacerte']");
var textFieldHtmlBody = textFieldHtml.html();
window.console && console.log(textFieldHtmlBody);

var textFieldValue = $.trim(stripHtml(textFieldHtmlBody));
if(textFieldValue) {
   window.console && console.log(textFieldValue);
   //return false; 
}
else
   window.console && console.log('Null or undefined');
1
  • it worked for descriptionField as (descriptionField.val() ==" "), moreover even description field is not blank , still alert pops out
    – 1592boy
    Jun 23, 2016 at 10:29

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.