esri/dijit/Geocoder - How to Set Max Characters allowed

2918
4
Jump to solution
02-17-2015 09:38 AM
JohnRitsko
New Contributor III

I've read the API content for the esri/dijit/Geocoder and didn't see anything in regards to maxCharacters allowed for user input.  Does anyone know how I can set this in my application?  One of our network staff tossed in roughly a million characters into the search box and slowed the server to a crawl for a few seconds.  Is there any way we can set a Max Characters sort of like the way there is a minCharacters property in the geocoder api?

Thanks so much.  Here is the code the way I have it now.

// Creates the Geocoder using the RTC Streets Geocoder

  var myGeocoders = [{

    url: locatorUrl,

    name: "LocateLocationByAddress",

    placeholder: "Search By Local Address"

  }];

  var geocoder = new Geocoder({

    map: map,

    autoComplete: true,

    arcgisGeocoder: false,

    geocoders: myGeocoders

  }, "search");

  geocoder.startup();

     

  var geoSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE,
18,

    new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,

    new Color([255, 153, 153, 0.5]), 8),

    new Color([255, 0, 0])

  );

       

// Modify the symbol user to show geocoded location

  geocoder.on("select", function showLocation(evt){

    var point = evt.result.feature.geometry;

    var graphic = new esri.Graphic(point, geoSymbol);

    map.graphics.add(graphic);

  });

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ChrisSergent
Regular Contributor III

The element that you need to change is search input, so if you want to change the maxLength to say 50, you would include the following in your javaScript:

document.getElementById("search_input").maxLength = 50;

Here is a sample page with the app doing just that:

JS Bin - Collaborative JavaScript Debugging

View solution in original post

4 Replies
SteveCole
Frequent Contributor

Try this. First, add "dojo/_base/event" to your list of requires.

Now add this event handler on your geocoder div:

            on(dijit.byId('search'), 'keydown', function(e) {
                var theInput = dijit.byId('search').value;
                if (theInput.length > 5) {
                    event.stop(e);
                }
            });

Obviously, adjust the length to whatever you want. In my application, any additional characters beyond 5 stops the input in its tracks.

0 Kudos
ChrisSergent
Regular Contributor III

The element that you need to change is search input, so if you want to change the maxLength to say 50, you would include the following in your javaScript:

document.getElementById("search_input").maxLength = 50;

Here is a sample page with the app doing just that:

JS Bin - Collaborative JavaScript Debugging

SteveCole
Frequent Contributor

Chris Sergent‌ I originally tried setting maxLength but couldn't get that to work. Now looking at your example, I realize that I was applying it to the dijit's DIV ID and not specifically to the input box within the geocoder widget. Doh!

JohnRitsko
New Contributor III

Thanks Chris, that did the trick.  Much appreciated.