Limit textbox values to a column in a SP list

Jun 7, 2012 at 3:55 PM

Hi again

After successfully implementing the AutoComplete functionality, a user suggested that I limit the input values to the values of an existing SharePoint list, thus getting dropdown behaviour - the problem being that it is an almost endless dropdown.

My question is this: Is there any way to limit the input of a textbox to the values of a column in an existing SP list?

I cannot get any info on Infopath Form limitations / validations that meet these requirements. Hoping that jquery would come the the rescue again.

Thanks

Coordinator
Jun 7, 2012 at 4:08 PM

Hein:

Sure. You can add any filtering you'd like in the CAMLQuery which retrieves the values. The other thing you can adjust is how many characters the user must type before they see results. With many longer lists of values, it makes more sense to set this to 2 or 3.

M.

Jun 8, 2012 at 10:08 AM

Hi

The CAML filters results that you get when entering information in the textbox - it does not force the user to enter a value that it finds in the list (if I am not mistaken)

What I am looking for is if a user enters a value that does not exist in the source list, the user gets an error / pop up etc.

Any ideas? I am currently looking at .inArray

Jun 8, 2012 at 1:46 PM

I may be oversimplifying this but in SharePoint you can already create a column whose value is a lookup from a column in the same list or a different list within the site.  If the lookup column contains up to 20 items, the control on your form will be rendered as a simple <select> object.  if the lookup column contains more than 20 items (as yours will), the control on your form will be rendered as a text box (which is what you want in this case).  Now, I know it may still look like a dropdown (<select> object) but it's really not that basic at that point.  It's actually a text box with an icon of a dropdown arrow attached to the right edge of it.  SharePoint designed that functionality to deal with large lookup lists and figures out which control to render on the fly based on the count of items in the lookup.  Rather than have a user launch a simple <select> control for large lookup lists, the user can just begin typing the option they want in the text box.  The backend scripts attached to the control will automatically begin showing the user a filtered list of options from the lookup list based on what is in the text box.  That filtered list is rendered as a separate object on the bottom edge of the text box.  SharePoint still enforces a valid lookup value from the source list.  The dropdown arrow attached to the right edge is there as an indicator to the user that there is a choice and also a convenience factor for the user if tey don't know what they're looking for.  Even after you click the arrow to display the choices, you can still begin typing in the text box to filter the list that appears and make a selection.

FYI, if you intend to perform any automation using form data in this type of control, be aware the control will dynamically render as either a <select> object or an <input> object, depending on how many items are in the lookup.  If your code isn't written to deal with that, you may get inconsistent results.  ...yes, I've been bitten by that feature more than once  ;-)

Coordinator
Jun 8, 2012 at 2:12 PM

Based on what I understand of your requirements, I think gkoliver is right. Wouldn't using the out of the box Lookup column functionality to give you a dropdown provide what you're lookng for?

M.

Jun 8, 2012 at 3:29 PM
function ReallyValidate(){
	
	this.fieldText = this.field.attr("value");

	var result = $.inArray(fieldText,this.arrayForTextBox);
				
		if ((result == -1) && (field.attr("value") != ""))
			{
			
			alert("The " + typeToValidate + " does not exist");

			field.attr("value","");
			
			this.formFieldToFocus = field;
			
			window.setTimeout(SetFocus, 100);

			}

 

Not exactly, but I did manage to get this done with .inArray

here is a snip of the code, one of the senior devs at my work used to do alot of JS and JQuery, he helped out

Sep 4, 2013 at 9:04 PM
I need to the same as above because I can not use a lookup column. I have the autocomplete working. I am not sure how this code should be used. I am not a developer but can usually do great with copy pasting and adapting code. Please help me get on track. Thanks
Coordinator
Sep 9, 2013 at 3:02 PM
My guess is that gkoliver would have used the function above either in the PreSaveAction or on the field's blur event. While you may be able to pull this off with some copy/paste action, you do need to understand those two ideas.

M.