ArcGIS Desktop

  • Documentation
  • Support

  • My Profile
  • Help
  • Sign Out
ArcGIS Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS Desktop

A complete professional GIS

ArcGIS Enterprise

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

ArcMap

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • Extensions

ValidateFieldName

  • Summary
  • Syntax
  • Code sample

Summary

Takes a string (field name) and a workspace path and returns a valid field name based on name restrictions in the output geodatabase. All invalid characters in the input string will be replaced with an underscore (_). The field name restrictions depend on the specific database used (Structured Query Language [SQL] or Oracle).

Syntax

ValidateFieldName (name, {workspace})
ParameterExplanationData Type
name

The field name to be validated. If the optional workspace is not specified, the field name is validated against the current workspace.

String
workspace

An optional specified workspace to validate the field name against. The workspace can be a file system or a personal, file, or enterprise geodatabase.

If the workspace is not specified, the field name is validated using the current workspace environment. If the workspace environment has not been set, the field name is validated based on a folder workspace.

String

Return Value

Data TypeExplanation
String

Returns a string containing the valid field name, based on either the current or specified workspace.

Code sample

ValidateFieldName example

Returns a valid field name based on the workspace.

import arcpy
import os


class ShapeError(Exception):
    pass

try:
    # Get the input feature class and make sure it contains polygons.
    #
    in_fc = arcpy.GetParameterAsText(0)
    if arcpy.Describe(input).shapeType != "Polygon":
        # Raise a custom exception
        raise ShapeError("Input does not contain polygons")

    # Get the new field name and validate it.
    #
    field_name = arcpy.GetParameterAsText(1)
    field_name = arcpy.ValidateFieldName(field_name, os.path.dirname(in_fc))

    # Add the new field and calculate the value.
    #
    arcpy.AddField_management(in_fc, field_name, "DOUBLE")
    arcpy.CalculateField_management(in_fc,
                                    field_name,
                                    "!shape.area! / !shape.length!",
                                    "PYTHON_9.3")
except ShapeError as err:
    print(err[0])
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

Related topics

  • ValidateTableName
  • ParseFieldName
  • Validating table and field names in Python

ArcGIS Desktop

  • Home
  • Documentation
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Insiders Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
Copyright © 2017 Esri. | Privacy | Legal