Question

I have a choice field in a SharePoint 2010 List which has its first choice as Please Select. Now I want the functionality that we can validate if a user has selected any option apart from Please select i.e a user should not select "please select" as an option. It should show an error when that happens. It will be a great help if i could get the javascript.

Thanks in advance

Was it helpful?

Solution

You can check if the selected values text is equal to 'please select' and if so display an alert to the user telling them to select a value with the following code:

function validateForm() { 
    var elem = document.getElementById("yourElementID");
    var selectedText = elem.options[elem.selectedIndex].text;
    if (selectedText == "please select") {
        alert("Please select a value");
        return false;
    }
}

The return false; is important if you are attaching this code to a form submission for validation as it will prevent the form from being submitted.

You can attach this function to your forms onSubmit event by doing the following:

<form action="..." method="..." onsubmit="return validateForm()">

OTHER TIPS

Its good solution explain in MitchS answer.but if you want to implement on ListItem Form like as NewForm, EditForm before Saving the item. than you should use PreSaveAction()

 function PreSaveAction()   
   {
     if($("select[title='My Choice'] option:selected").val() == '')
     {
        alert("Please select.")
        return false;
     }
    else
    {
    return true;
    }

  }

blow script find your choice dropdownlist control based on his title attribute.

Hops its helps!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top