Don't assume that the input being validated is a string

An integer value will throw an error. Convert to a string before performing regex