Captain Comic
Captain Comic

Reputation: 16196

Need to validate configuration property - must be 20 digits number

The user of my .NET application must provide 20-digit account number in the application configuration file. I am derived class from ConfigurationSection to work with custom sections.

[ConfigurationProperty("RECEIVED_ACCOUNT", IsRequired = true)]
public string RECEIVED_ACCOUNT
{
    get { return (string)this["RECEIVED_ACCOUNT"]; }
    set { this["RECEIVED_ACCOUNT"] = value; }
}

I could use StringValidator. It provides MaxLength, MinLength and InvalidCharacters. But it does not allow to limit allowed characters to 0-9 w

Upvotes: 1

Views: 635

Answers (1)

Rob Levine
Rob Levine

Reputation: 41298

I would suggest using a Regular Expression Validator and setting the ValidationExpresison property to be

^\d{20}$

This will validate a number of exactly 20 digits:

  • ^ means match the start of the string
  • \d means match digits only
  • {20} means match exactly 20 characters (of the digit specified previously)
  • $ means match the end of the string

Upvotes: 3

Related Questions