0

My SharePoint 2010 list has a choice column that with checkboxes and allows multiple selections. For all these checkbox field choices there is a corresponding textbox fields are created. This textbox field is of type SPNumberField.

I want to validate these textboxes whenever user makes a check in the checkbox for null values.

I mean, assuming if the choice field values are:

  • MS
  • IBM
  • SAP
  • ORACLE
  • SALESFORCE

and its corresponding textbox fields are "MS", "IBM" ...

If a user selects IBM and if the "IBM textbox" is empty, the user should not be able to save the form.

I tried using the spUtility and pure JavaScript, but it validates only for one checkbox which is selected by default, when the mycustomnewform.aspx is loaded.

When I selected other checkboxes and click the save button, the code is not getting executed. my below code:

 var compStatusChoiceField = SPUtility.GetSPField('Company');
 var compStatusChoiceFieldValue = compStatusChoiceField.GetValue();
 alert(compStatusChoiceFieldValue + '  == is the company value');
 var varIBM = SPUtility.GetSPField('IBM').GetValue();

 if(compStatusChoiceFieldValue == 'IBM')
        {
     if(varIBM == null || varIBM.length === 0)
      {
        flagGeneric = false;
        alert('IBM txtbox value is a mandatory!');
      }
   }
1

You may use a simple JavaScript to find whether the checkbox is checked, if yes, find the corresponding textbox and check if the textbox contains any value. Show an alert message to the users if there is no value.

Given below a sample JS code, modify the code based on your HTML format

<script type="text/javascript" language="javascript">
function PreSaveAction() {
var isValid="";

if(document.getElementById("#CheckBox1ID").checked == true)
{
var validationa = $('#TextBoxForCheckBox1ID').val();
if(validationa.length==0){
isValid += "CheckBox1";
}
}
if(document.getElementById("#CheckBox2ID").checked == true)
{
var validationb = $('#TextBoxForCheckBox2ID').val();
if(validationb.length==0){
isValid += "CheckBox2";
}
}
if(document.getElementById("#CheckBox3ID").checked == true)
{
var validationc = $('#TextBoxForCheckBox3ID').val();
if(validationc.length==0){
isValid += "CheckBox3";
}
}

if(isValid.length>0)
{
alert("This textbox corresponding to selected checkbox cannot be empty");
return false;
}

return true;
}
</script>
  • Nagarajan, I already did the same , but its working for only ONE choice field. When I select multiselect check boxes and tried for the validation , it failed to validate other text boxes.I added the code, that I tried.Can you tell,how to get the ID of the ChoiceField? I do not want to take the ID which is generated at the viewsource or F12 Developer Tools as hardcoding that ID is not a recommended practice. – userAZLogicApps Oct 16 '15 at 9:20
  • Try using the updated code – Nagarajan Muthukumar Oct 16 '15 at 10:11
  • Can you tell, how can I get the #CheckBox1ID ? – userAZLogicApps Oct 16 '15 at 10:25
  • You can get it from your HTML content. Else, provide the html content of table where checkboxes and textbox are present. – Nagarajan Muthukumar Oct 16 '15 at 10:34
  • geekswithblogs.net/SoYouKnow/archive/2011/08/20/… this is helping me! – userAZLogicApps Oct 17 '15 at 13:56

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.