Table of Contents

Class ValidationHelper

Namespace
CMS.Helpers
Assembly
CMS.Helpers.dll

Global class providing Validation methods.

public class ValidationHelper
Inheritance
object
ValidationHelper
Extension Methods

Fields

APP_SETTINGS_HASH_STRING_SALT

App setting key for hash string salt.

public const string APP_SETTINGS_HASH_STRING_SALT = "CMSHashStringSalt"

Field Value

string

CMS_SETTINGS_JWT_TOKEN_ENCRYPTION_KEY

CMS setting key for JWT token encryption security key.

public const string CMS_SETTINGS_JWT_TOKEN_ENCRYPTION_KEY = "CMSJwtTokenEncryptionKey"

Field Value

string

EMAIL_DOMAIN_LENGTH

Maximal length of email domain (email without the section before the "@").

public const int EMAIL_DOMAIN_LENGTH = 252

Field Value

int

Remarks

According to RFC 5321, email addresses should be at most 254 characters long. For the maximal domain length, another two characters must be substracted (@ and at least one character for the local part of the email).

MULTIPLE_EMAILS_LENGTH

Maximal length of multiple emails.

public const int MULTIPLE_EMAILS_LENGTH = 998

Field Value

int

Remarks

According to RFC 2822 line length must be no more than 998 characters long. As multiple email addresses are in one line, this length should not exceed.

SINGLE_EMAIL_LENGTH

Maximal length of email.

public const int SINGLE_EMAIL_LENGTH = 254

Field Value

int

Remarks

According to RFC 5321, email addresses should be at most 254 characters long.

Properties

CustomEmailRegExpString

Custom email regular expression string from web.config for email validation.

public static string CustomEmailRegExpString { get; }

Property Value

string

HashStringSalt

Returns salt used for hashing string in GetHashString() method.

public static string HashStringSalt { get; set; }

Property Value

string

JwtTokenEncryptionKey

JWT token encryption security key.

[Obsolete("Property was not intended for public use and will be removed.")]
[ObsoleteSince(29, 6)]
public static string JwtTokenEncryptionKey { get; set; }

Property Value

string

Methods

AreEmails(string, string, bool)

Returns true if the object representation matches the Email list separated by specified character (default separator is semicolon).

public static bool AreEmails(string value, string separator = null, bool checkLength = false)

Parameters

value string

Value to check

separator string

String that delimits the addresses

checkLength bool

if true checks whether value length is no longer than MULTIPLE_EMAILS_LENGTHand each individual e-mail is no longer than SINGLE_EMAIL_LENGTH

Returns

bool

GetBinary(object, byte[], CultureInfo)

Returns the byte[] representation of an object or default value if not.

public static byte[] GetBinary(object value, byte[] defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue byte[]

Default value

culture CultureInfo

Culture used for conversion

Returns

byte[]

GetBoolean(object, bool, CultureInfo)

Returns the boolean representation of an object or default value if not.

public static bool GetBoolean(object value, bool defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue bool

Default value

culture CultureInfo

Culture used for conversion

Returns

bool

GetCodeName(string, int)

Gets the code name created from the given string.

public static string GetCodeName(string name, int maxLength)

Parameters

name string

Display name

maxLength int

Maximal length of the codename

Returns

string

GetCodeName(string, string, int, bool, bool, string, bool)

Gets the code name created from the given string.

public static string GetCodeName(string name, string replacement = null, int maxLength = 0, bool useUnicode = true, bool removeDiacritics = true, string allowedCharacters = null, bool useCamelCase = true)

Parameters

name string

Display name

replacement string

Replacement string for invalid characters

maxLength int

Maximal length of the codename

useUnicode bool

If true, Unicode letters are allowed in the codename, otherwise only a-Z are allowed

removeDiacritics bool

If true, diacritics is removed from Latin characters

allowedCharacters string

Characters which should be allowed within the current name

useCamelCase bool

If true, camel case is used for the code name

Returns

string

GetDate(object, DateTime)

Returns the DateTime representation of an object or default value if not.

public static DateTime GetDate(object value, DateTime defaultValue)

Parameters

value object

Value to convert

defaultValue DateTime

Default value

Returns

DateTime

GetDate(object, DateTime, CultureInfo)

Returns the DateTime representation of an object or default value if not.

public static DateTime GetDate(object value, DateTime defaultValue, CultureInfo culture)

Parameters

value object

Value to convert

defaultValue DateTime

Default value

culture CultureInfo

Culture to use

Returns

DateTime

GetDateTime(object, DateTime, CultureInfo)

Returns the DateTime representation of an object or default value if not.

public static DateTime GetDateTime(object value, DateTime defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue DateTime

Default value

culture CultureInfo

Culture for the conversion. If null, English culture is used.

Returns

DateTime

GetDateTime(object, DateTime, string)

Returns the DateTime representation of an object or default value if not.

public static DateTime GetDateTime(object value, DateTime defaultValue, string culture)

Parameters

value object

Value to convert

defaultValue DateTime

Default value

culture string

Culture to use for processing of the string

Returns

DateTime

GetDateTimeSystem(object, DateTime)

Returns the DateTime representation of an object (using English representation of date and time) or default value if not.

public static DateTime GetDateTimeSystem(object value, DateTime defaultValue)

Parameters

value object

Value to convert

defaultValue DateTime

Default value

Returns

DateTime

GetDecimal(object, decimal, CultureInfo)

Returns the decimal representation of an object or default value if not.

public static decimal GetDecimal(object value, decimal defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue decimal

Default value

culture CultureInfo

Culture for the conversion. If null, English culture is used.

Returns

decimal

GetDecimal(object, decimal, string)

Returns the decimal representation of an object or default value if not.

public static decimal GetDecimal(object value, decimal defaultValue, string culture)

Parameters

value object

Value to convert

defaultValue decimal

Default value

culture string

Culture code. If null, English culture is used.

Returns

decimal

GetDecimalSystem(object, decimal)

Converts the given object to a decimal value

public static decimal GetDecimalSystem(object value, decimal defaultValue)

Parameters

value object

Value to convert

defaultValue decimal

Default value

Returns

decimal

GetDouble(object, double, CultureInfo)

Returns the double representation of an object or default value if not.

public static double GetDouble(object value, double defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue double

Default value

culture CultureInfo

Culture info

Returns

double

GetDouble(object, double, string)

Returns the double representation of an object or default value if not.

public static double GetDouble(object value, double defaultValue, string culture)

Parameters

value object

Value to convert

defaultValue double

Default value

culture string

Culture code

Returns

double

GetDoubleSystem(object, double)

Returns the double representation of an object (using English representation of floating point numbers) or default value if not.

public static double GetDoubleSystem(object value, double defaultValue)

Parameters

value object

Value to convert

defaultValue double

Default value

Returns

double

GetFloat(object, float, CultureInfo)

Returns the double representation of an object or default value if not.

public static float GetFloat(object value, float defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue float

Default value

culture CultureInfo

Culture used for conversion

Returns

float

GetGuid(object, Guid, CultureInfo)

Returns the GUID representation of an object or default value if not.

public static Guid GetGuid(object value, Guid defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue Guid

Default value

culture CultureInfo

Culture used for conversion

Returns

Guid

GetHashString(string, HashSettings)

Gets the SHA2 hash for the given value (salt unique to certain user session and database is added).

public static string GetHashString(string value, HashSettings settings)

Parameters

value string

Value to hash

settings HashSettings

Hash settings

Returns

string

GetIdentifier(object, string)

Gets the identifier created from the given name.

public static string GetIdentifier(object name, string replacement = null)

Parameters

name object

Display name

replacement string

Replacement string for invalid characters

Returns

string

GetInteger(object, int, CultureInfo)

Returns the integer representation of an object or default value if not.

public static int GetInteger(object value, int defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue int

Default value

culture CultureInfo

Culture used for conversion

Returns

int

GetLong(object, long, CultureInfo)

Returns the long representation of an object or default value if not.

public static long GetLong(object value, long defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue long

Default value

culture CultureInfo

Culture used for conversion

Returns

long

GetSafeFileName(string, string)

Get safe file name in which forbidden characters are replaced with specified replacement

public static string GetSafeFileName(string value, string replacement = null)

Parameters

value string

File name

replacement string

Replacement string

Returns

string

GetString(object, string, CultureInfo)

Returns the string representation of an object or default value if not.

public static string GetString(object value, string defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue string

Default value

culture CultureInfo

Culture

Returns

string

GetString(object, string, string)

Returns the string representation of an object or default value if not.

public static string GetString(object value, string defaultValue, string culture)

Parameters

value object

Value to convert

defaultValue string

Default value

culture string

Culture

Returns

string

GetString(object, string, string, string)

Returns the string representation of an object or default value if not.

public static string GetString(object value, string defaultValue, string culture, string format)

Parameters

value object

Value to convert

defaultValue string

Default value

culture string

Culture code. If null, English culture is used.

format string

Formatting string

Returns

string

GetTimeSpan(object, TimeSpan, CultureInfo)

Returns the TimeSpan representation of an object or default value if not.

public static TimeSpan GetTimeSpan(object value, TimeSpan defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue TimeSpan

Default value

culture CultureInfo

Culture for the conversion. If null, English culture is used.

Returns

TimeSpan

GetTimeSpan(object, TimeSpan, string)

Returns the TimeSpan representation of an object or default value if not.

public static TimeSpan GetTimeSpan(object value, TimeSpan defaultValue, string culture)

Parameters

value object

Value to convert

defaultValue TimeSpan

Default value

culture string

Culture for the conversion

Returns

TimeSpan

GetTimeSpanSystem(object, TimeSpan)

Returns the TimeSpan representation of an object (using English representation of date and time) or default value if not.

public static TimeSpan GetTimeSpanSystem(object value, TimeSpan defaultValue)

Parameters

value object

Value to convert

defaultValue TimeSpan

Default value

Returns

TimeSpan

GetValue<ReturnType>(object)

Converts the value to specified type.

public static ReturnType GetValue<ReturnType>(object value)

Parameters

value object

Value to convert

Returns

ReturnType

Type Parameters

ReturnType

Result type

GetValue<T>(object, T, CultureInfo)

Converts the value to specified type. If the value is null, default value is used.

public static T GetValue<T>(object value, T defaultValue, CultureInfo culture = null)

Parameters

value object

Value to convert

defaultValue T

Default value

culture CultureInfo

Culture

Returns

T

Type Parameters

T

Result type

IsClassName(string)

Returns true if the object representation matches the CMS.Helpers.ValidationHelper.ClassNameRegExp.

public static bool IsClassName(string value)

Parameters

value string

Value to check.

Returns

bool

IsCodeName(object, bool)

Returns true if the object representation matches the Code name.

public static bool IsCodeName(object value, bool useUnicode = false)

Parameters

value object

Value to check

useUnicode bool

If true, unicode letters are allowed in the codename, otherwise only a-Z are allowed

Returns

bool

IsDateTime(object, CultureInfo)

Returns true if the object representation matches the date time format.

public static bool IsDateTime(object value, CultureInfo culture)

Parameters

value object

Value to check

culture CultureInfo

Culture for conversion

Returns

bool

IsDecimal(object, CultureInfo, int, int)

Returns true if the object representation matches the decimal type.

public static bool IsDecimal(object value, CultureInfo culture, int precision = 0, int scale = 0)

Parameters

value object

Value to check

culture CultureInfo

Culture code. If null, English culture is used.

precision int

Maximum number of digits

scale int

Maximum number of decimal places

Returns

bool

IsDouble(object, string)

Returns true if the object representation matches the Double type.

public static bool IsDouble(object value, string culture = null)

Parameters

value object

Value to check

culture string

Optional culture code. If null, English culture is used.

Returns

bool

IsEmail(string, bool)

Returns true if the object representation matches the Email.

public static bool IsEmail(string value, bool checkLength = false)

Parameters

value string

Value to check

checkLength bool

if true checks whether e-mail is no longer than SINGLE_EMAIL_LENGTH

Returns

bool

IsGuid(object)

Returns true if the object representation matches the Guid type.

public static bool IsGuid(object value)

Parameters

value object

Value to check

Returns

bool

IsIdentifier(object)

Returns true if the object representation matches the IdentifierRegExp.

public static bool IsIdentifier(object value)

Parameters

value object

Value to check

Returns

bool

IsInteger(object)

Returns true if the object representation matches the Integer type.

public static bool IsInteger(object value)

Parameters

value object

Value to check

Returns

bool

IsLong(object)

Returns true if the object representation matches the Long type.

public static bool IsLong(object value)

Parameters

value object

Value to check

Returns

bool

IsTimeSpan(object, CultureInfo)

Returns true if the object representation matches the TimeSpan type.

public static bool IsTimeSpan(object value, CultureInfo culture)

Parameters

value object

Value to check

culture CultureInfo

Culture for conversion

Returns

bool

LoadLong(ref long, object)

Attempts to load the given new value as a long to the result. Returns true if the conversion of the new value was successful, if not, returns false and keeps the value the same.

[Obsolete("Method was not intended for public use and will be removed in the next version.")]
[ObsoleteSince(28, 3)]
public static bool LoadLong(ref long value, object newValue)

Parameters

value long

Value to load

newValue object

New value

Returns

bool

ValidateHash(string, string, string, string)

Validates the hash for the given value.

public static bool ValidateHash(string value, string hash, string purpose, string hashSalt = "")

Parameters

value string

Value

hash string

Hash

purpose string

Purpose of the hash

hashSalt string

Custom hash salt

Returns

bool