1

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[6].altered and not self.params[6].hasBeenValidated:
      if self.params[6].value >= '10000':
        self.params[6].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[6].value >= '10000':
      self.params[6].setErrorMessage ("Project number can only have 4 positive digits. Please enter a 4-digit project number.")
    else:
      self.params[6].clearMessage()
    return
1

Below is the code you need in updateMessages() :

It would check if an alpha numeric code is 4 characters long and a numeric code must not be greater than 9999. The key function is isdigit() this allows you to split off the logic for numeric and alphanumeric codes.

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if self.params[0].altered:
        theID = self.params[0].value # could be something like ab12 or 3330
        if theID.isdigit():
            # Ensure number (assumed integer) is not greater than 9999
            num = int(theID)
            if num >= 10000:
                self.params[0].setErrorMessage("Project ID is invalid!")
            else:
                self.params[0].clearMessage()
        else:
            # Ensure alpha numeric ID is exactly 4 characters long
            if len(theID) != 4 :
                self.params[0].setErrorMessage("Project ID is invalid!")
            else:
                self.params[0].clearMessage()
    return
  • 1
    This worked for me. With the modification of one line of code to if num < 1000 or num >= 10000:, I was able to make sure an entered number was exactly 4 digits. – CPeters Jan 4 '18 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.