Validating Symbol Table Names in AutoCAD

If you are writing a lisp routine and you have to ask the user for a new symbol table name, such as the name of a layer or dimstyle, how can you make sure the user gives you a valid name? What would happen if you ask the user for a layer name and he/she enters *Mile (maybe it was a typo and they meant 8Mile). As soon as you pass that string to a function or command that tries to create that layer, it will fail.

Do you need to write your own routine to check strings? No, autolisp includes a function named (snvalid) that will validate symbol names for you. Simply call the function like this (snvalid sym) where sym is the string representing the symbol name. There are two important things to know about using this function. First, this function looks at the value of EXTNAMES to determine what rules to use in validation. Second, there is an optional flag you can use in the function if you want to allow the pipe symbol (or a vertical bar) in the symbol name (excluding this character being the first or last character in the symbol name – which is never allowed). (snvalid) will return T if the name is valid or nil if the name is not valid.

Here are some examples:

(setvar "EXTNAMES" 1)
(snvalid "crazy layer$%&")
T
(snvalid "crazy layer$%&?")
nil
(snvalid "crazy|layer" 1)
T
(setvar "EXTNAMES" 0)
(snvalid "crazy layer")
nil

Detailed documentation on SNVALID, including characters that are never allowed, and more examples.