Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:16:03+00:00 2026-05-16T10:16:03+00:00

I have the following code to validate some fields on a sharepoint newform.aspx. To

  • 0

I have the following code to validate some fields on a sharepoint newform.aspx. To not submit the form in sharepoint I must return a false statement to the default function PreSaveItem.

Validation:

//bind a change event to all controls to validate
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").change(function(){
        checkControls()
    });

    //the change event function - check the status of each control
    function checkControls(){

    //set a variable to count the number of valid controls
    var controlsPassed = 0;

    //set up a selector to pick .each() of the target controls
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){

        //if the control value is not zero AND is not zero-length
        var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl00_UserField_hiddenSpanData').val();
        var val = $(this).val();
        if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });

    //call the PreSaveItem function and pass the true/false statement of 5 valid controls

return (controlsPassed == 5) 

    }
        function PreSaveItem() {
            return checkControls()
    }

I want to validate different elements depending on what I have selected in a dropdown list called Item Level.

I get the values from Item Level with:

$("select[title='Item Level']").change(function() {
        var itemLevel = $(this).val();      
        if (itemLevel == "Strategic Objective") {


        alert(itemLevel);
        }
        if (itemLevel == "Strategic Priority") {


        alert(itemLevel);
        }
        if (itemLevel == "Milestone Action") {


        alert(itemLevel);
        }
        if (itemLevel == "Performance Measure") {


        alert(itemLevel);
        }

        });

I thought it would be easy to just chuck the validation code into the if’s but it doesn’t work.

For example:

        $("select[title='Item Level']").change(function() {
        var itemLevel = $(this).val();      
        if (itemLevel == "Strategic Objective") {


        alert(itemLevel);
        }
        if (itemLevel == "Strategic Priority") {


        alert(itemLevel);
        }
        if (itemLevel == "Milestone Action") {
        //bind a change event to all controls to validate
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").change(function(){
        checkControls()
    });

    //the change event function - check the status of each control
    function checkControls(){

    //set a variable to count the number of valid controls
    var controlsPassed = 0;

    //set up a selector to pick .each() of the target controls
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){

        //if the control value is not zero AND is not zero-length
        var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl00_UserField_hiddenSpanData').val();
        var val = $(this).val();
        if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });

    //call the PreSaveItem function and pass the true/false statement of 5 valid controls

return (controlsPassed == 5) 

    }
        function PreSaveItem() {
            return checkControls()
    }


        alert(itemLevel);
        }
        if (itemLevel == "Performance Measure") {


        alert(itemLevel);
        }

        });

and in the item level item Strategic Objective validate some other elements. Any ideas?

Thanks in advance.

Edit: the working code with help from casablanca:

function checkControls() {
  var itemLevel = $("select[title='Item Level']").val();
  switch (itemLevel) {
    case 'Strategic Objective':

    var controlsPassed = 0;

    $("input[id$=UserField_hiddenSpanData]").each(function(){

        var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val();
        var val = $(this).val();
        if(val != 0 && val.length != 0 && txt.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
        return (controlsPassed == 1) 

    case 'Milestone Action':

      var controlsPassed = 0;

    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){

        var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val();
        var val = $(this).val();
        if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
        return (controlsPassed == 5) 

    case 'Performance Measure':

        var controlsPassed = 0;

    $("select[title=Strategic Objective],select[title=Strategic Priority]").each(function(){

        var val = $(this).val();
        if(val != 0 && val.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
    return (controlsPassed == 2) 

case 'Strategic Priority':

        var controlsPassed = 0;

    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective]").each(function(){

        var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val();
        var val = $(this).val();
        if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
    return (controlsPassed == 4) 

  }
}

function PreSaveItem() 
    {
            return checkControls()
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:16:03+00:00Added an answer on May 16, 2026 at 10:16 am

    Okay, so this is the problem I see in your current script: binding an event handler is permanent, so if you choose “Milestone Action” even once and then switch to another option, you will still validate the original selection.

    You can do away with all the change handlers and instead add your conditions directly in checkControls, validating the necessary elements based on the current selection. I’ve also replaced your ifs with a switch since it’s cleaner:

    function checkControls() {
      var itemLevel = $("select[title='Item Level']").val();
      switch (itemLevel) {
        case 'Strategic Objective':
          // Perform validation for this item level
          return result; // true or false
    
        case 'Milestone Action':
          // Perform validation for this item level
          return result; // true or false
    
        // etc...
      }
    }
    

    Update: For example, if when “Milestone Action” is selected, you would like to check if some text field is not empty, then under case 'Milestone Action':, you would add something like this:

    if ($('something').val().length > 0)
      return true;
    else
      return false;
    

    Edit: If you have access to the form code (I’m not familiar with SharePoint), you should assign IDs to your form elements so you can reference them like $('#itemLevel') instead of having to use a long selector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate the following form. Some fields are just required, however the
I have a simple signup form with the following jQuery validation code: $(document).ready(function(){ $(#registerForm).validate({
I have the following block of code: $(document).ready(function(){ $(#form).submit(function(ev) { ev.preventDefault(); $.ajax({ type: POST,
I have the following code to validate two text box entries to make sure
I have the following code using jQuery Validate. $(#register).validate({ debug: true, errorClass:'error', validClass:'success', errorElement:'span',
I have the following code to enable validation work: $(document).ready(function() { $(#eventForm).validate({ rules: {
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS
Some people have told me that the following code is bad for HTML validation:
I have the following html code : <form name=uploadForm action= method=POST enctype=multipart/form-data> <input type=file
I have the following code to upload a file to the server. For some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.