Class ValidationHelper
Global class providing Validation methods.
public class ValidationHelper
- Inheritance
-
objectValidationHelper
- Extension Methods
Constructors
ValidationHelper()
public ValidationHelper()
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.")]
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
valuestringValue to check
separatorstringString that delimits the addresses
checkLengthboolif
truechecks whethervaluelength 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
valueobjectValue to convert
defaultValuebyte[]Default value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValueboolDefault value
cultureCultureInfoCulture 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
namestringDisplay name
maxLengthintMaximal 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
namestringDisplay name
replacementstringReplacement string for invalid characters
maxLengthintMaximal length of the codename
useUnicodeboolIf true, Unicode letters are allowed in the codename, otherwise only a-Z are allowed
removeDiacriticsboolIf true, diacritics is removed from Latin characters
allowedCharactersstringCharacters which should be allowed within the current name
useCamelCaseboolIf 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
valueobjectValue to convert
defaultValueDateTimeDefault 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
valueobjectValue to convert
defaultValueDateTimeDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValueDateTimeDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValueDateTimeDefault value
culturestringCulture 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
valueobjectValue to convert
defaultValueDateTimeDefault 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
valueobjectValue to convert
defaultValuedecimalDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValuedecimalDefault value
culturestringCulture 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
valueobjectValue to convert
defaultValuedecimalDefault 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
valueobjectValue to convert
defaultValuedoubleDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValuedoubleDefault value
culturestringCulture 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
valueobjectValue to convert
defaultValuedoubleDefault 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
valueobjectValue to convert
defaultValuefloatDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValueGuidDefault value
cultureCultureInfoCulture 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
valuestringValue to hash
settingsHashSettingsHash settings
Returns
- string
GetIdentifier(object, string)
Gets the identifier created from the given name.
public static string GetIdentifier(object name, string replacement = null)
Parameters
nameobjectDisplay name
replacementstringReplacement 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
valueobjectValue to convert
defaultValueintDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValuelongDefault value
cultureCultureInfoCulture 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
valuestringFile name
replacementstringReplacement 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
valueobjectValue to convert
defaultValuestringDefault value
cultureCultureInfoCulture
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
valueobjectValue to convert
defaultValuestringDefault value
culturestringCulture
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
valueobjectValue to convert
defaultValuestringDefault value
culturestringCulture code. If null, English culture is used.
formatstringFormatting 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
valueobjectValue to convert
defaultValueTimeSpanDefault value
cultureCultureInfoCulture 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
valueobjectValue to convert
defaultValueTimeSpanDefault value
culturestringCulture 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
valueobjectValue to convert
defaultValueTimeSpanDefault value
Returns
- TimeSpan
GetValue<ReturnType>(object)
Converts the value to specified type.
public static ReturnType GetValue<ReturnType>(object value)
Parameters
valueobjectValue to convert
Returns
- ReturnType
Type Parameters
ReturnTypeResult 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
valueobjectValue to convert
defaultValueTDefault value
cultureCultureInfoCulture
Returns
- T
Type Parameters
TResult type
IsClassName(string)
Returns true if the object representation matches the CMS.Helpers.ValidationHelper.ClassNameRegExp.
public static bool IsClassName(string value)
Parameters
valuestringValue 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
valueobjectValue to check
useUnicodeboolIf 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
valueobjectValue to check
cultureCultureInfoCulture 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
valueobjectValue to check
cultureCultureInfoCulture code. If null, English culture is used.
precisionintMaximum number of digits
scaleintMaximum 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
valueobjectValue to check
culturestringOptional 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
valuestringValue to check
checkLengthboolif
truechecks 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
valueobjectValue to check
Returns
- bool
IsIdentifier(object)
Returns true if the object representation matches the IdentifierRegExp.
public static bool IsIdentifier(object value)
Parameters
valueobjectValue to check
Returns
- bool
IsInteger(object)
Returns true if the object representation matches the Integer type.
public static bool IsInteger(object value)
Parameters
valueobjectValue to check
Returns
- bool
IsLong(object)
Returns true if the object representation matches the Long type.
public static bool IsLong(object value)
Parameters
valueobjectValue 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
valueobjectValue to check
cultureCultureInfoCulture 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.")]
public static bool LoadLong(ref long value, object newValue)
Parameters
valuelongValue to load
newValueobjectNew 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
valuestringValue
hashstringHash
purposestringPurpose of the hash
hashSaltstringCustom hash salt
Returns
- bool