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;
}
}
3 Answers
You have two problems:
- Your
txtTitle1
variable is equal to$.(
. Just remove that dot after the dollar sign. - Your Description field seems to contain rich HTML because the field ID in the DOM contains the word
_inplacerte
at the end, therefore thevalue()
jQuery function will not work since the field is not aninput
ortextarea
element, but rather aDIV
element. In this case you need to replacevalue()
withtext().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;
}
-
i am getting alert for application name but when i check for description it fails– 1592boyCommented 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.– MihailCommented Jun 22, 2016 at 14:19
-
still not getting alert box message when description field is left blank– 1592boyCommented 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.– MihailCommented 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.– MihailCommented Jun 22, 2016 at 15:23
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;
}
}
-
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.– MihailCommented Jun 22, 2016 at 14:11
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');
-
it worked for descriptionField as (descriptionField.val() ==" "), moreover even description field is not blank , still alert pops out– 1592boyCommented Jun 23, 2016 at 10:29
.
after $