I have a script that takes user input. For the "Project ID" input field, the user can only enter a 4 character ID. The ID may include letters and numbers, but is typically a 4-digit number. I'm trying to use the script's ToolValidator to make sure the user enters only 4 characters before the script will run. I am using the correct parameter index #.
I've tried several different code variations, but this is an example of my last attempt. With the following code, no matter what value I enter, the "Project ID" field returns blank with no error message.
Also, the "Project ID" parameter is a STRING data type in the event we encounter a project ID that contains a letter and is a required field. I would normally just check the length of the input, but couldn't find a len() method to use. I figured I could test if the entry was less/greater than 10000 to obtain the 4 digit restriction.
How should I proceed?
import arcpy class ToolValidator(object): """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup arcpy and the list of tool parameters.""" self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" if self.params.altered and not self.params.hasBeenValidated: if self.params.value >= '10000': self.params.value = "" return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" if self.params.value >= '10000': self.params.setErrorMessage ("Project number can only have 4 positive digits. Please enter a 4-digit project number.") else: self.params.clearMessage() return