From 3fe63c3c534433edd4abbc2ae8fd300bb510c0e0 Mon Sep 17 00:00:00 2001 From: Ronen Date: Thu, 28 Sep 2023 23:47:15 +0300 Subject: [PATCH] arranged text validators --- .../Source/Entities/TextInputValidators.cs | 317 ------------------ .../TextValidators/EnglishCharactersOnly.cs | 76 +++++ .../TextValidators/FilenameValidator.cs | 76 +++++ .../Entities/TextValidators/ITextValidator.cs | 38 +++ .../Entities/TextValidators/MakeTitleCase.cs | 45 +++ .../Entities/TextValidators/NumbersOnly.cs | 107 ++++++ .../TextValidators/OnlySingleSpaces.cs | 40 +++ .../Entities/TextValidators/SlugValidator.cs | 76 +++++ README.md | 3 + 9 files changed, 461 insertions(+), 317 deletions(-) delete mode 100644 GeonBit.UI/Source/Entities/TextInputValidators.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/EnglishCharactersOnly.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/FilenameValidator.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/ITextValidator.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/MakeTitleCase.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/NumbersOnly.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/OnlySingleSpaces.cs create mode 100644 GeonBit.UI/Source/Entities/TextValidators/SlugValidator.cs diff --git a/GeonBit.UI/Source/Entities/TextInputValidators.cs b/GeonBit.UI/Source/Entities/TextInputValidators.cs deleted file mode 100644 index 19474ec..0000000 --- a/GeonBit.UI/Source/Entities/TextInputValidators.cs +++ /dev/null @@ -1,317 +0,0 @@ -#region File Description -//----------------------------------------------------------------------------- -// Validators you can attach to TextInput entities to manipulate and validate -// user input. These are used to create things like text input for numbers only, -// limit characters to english chars, etc. -// -// Author: Ronen Ness. -// Since: 2016. -//----------------------------------------------------------------------------- -#endregion - -namespace GeonBit.UI.Entities.TextValidators -{ - /// - /// GeonBit.UI.Entities.TextValidators contains different text validators and processors you can attach to TextInput entities. - /// - [System.Runtime.CompilerServices.CompilerGenerated] - class NamespaceDoc - { - } - - /// - /// A class that validates text input to make sure its valid. - /// These classes can be added to any TextInput to limit the type of input the user can enter. - /// Note: this cannot be an interface due to serialization. - /// - public partial class ITextValidator - { - /// - /// Get the new text input value and return true if valid. - /// This function can either return false to scrap input changes, or change the text and return true. - /// - /// New text input value. - /// Previous text input value. - /// If TextInput value is legal. - public virtual bool ValidateText(ref string text, string oldText) { return true; } - } - - /// - /// Make sure input is numeric and optionally validate min / max values. - /// - [System.Serializable] - public class TextValidatorNumbersOnly : ITextValidator - { - /// - /// Static ctor. - /// - static TextValidatorNumbersOnly() - { - Entity.MakeSerializable(typeof(TextValidatorNumbersOnly)); - } - - /// - /// Do we allow decimal point? - /// - public bool AllowDecimalPoint; - - /// - /// Optional min value. - /// - public double? Min; - - /// - /// Optional max value. - /// - public double? Max; - - /// - /// Create the number validator. - /// - /// If true, will allow decimal point in input. - /// If provided, will force min value. - /// If provided, will force max value. - public TextValidatorNumbersOnly(bool allowDecimal, double? min = null, double? max = null) - { - AllowDecimalPoint = allowDecimal; - Min = min; - Max = max; - } - - /// - /// Create number validator with default params. - /// - public TextValidatorNumbersOnly() : this(false) - { - } - - /// - /// Return true if text input is a valid number. - /// - /// New text input value. - /// Previous text input value. - /// If TextInput value is legal. - public override bool ValidateText(ref string text, string oldText) - { - // if string empty return true - if (text.Length == 0) - { - return true; - } - - // will contain value as number - double num; - - // try to parse as double - if (AllowDecimalPoint) - { - if (!double.TryParse(text, out num)) - { - return false; - } - } - // try to parse as int - else - { - int temp; - if (!int.TryParse(text, out temp)) - { - return false; - } - num = temp; - } - - // validate range - if (Min != null && num < (double)Min) { text = Min.ToString(); } - if (Max != null && num > (double)Max) { text = Max.ToString(); } - - // valid number input - return true; - } - } - - /// - /// Make sure input contains only english characters. - /// - [System.Serializable] - public class TextValidatorEnglishCharsOnly : ITextValidator - { - /// - /// Static ctor. - /// - static TextValidatorEnglishCharsOnly() - { - Entity.MakeSerializable(typeof(TextValidatorEnglishCharsOnly)); - } - - // the regex to use - System.Text.RegularExpressions.Regex _regex; - - // regex for english only with spaces - static readonly System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|]+$"); - - // regex for english only without spaces - static readonly System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|\ ]+$"); - - // do we allow spaces in text? - private bool _allowSpaces; - - /// - /// Set / get if we allow spaces in text. - /// - public bool AllowSpaces - { - get { return _allowSpaces; } - set { _allowSpaces = value; _regex = _allowSpaces ? _slugWithSpaces : _slugNoSpaces; } - } - - /// - /// Create the validator. - /// - /// If true, will allow spaces. - public TextValidatorEnglishCharsOnly(bool allowSpaces) - { - AllowSpaces = allowSpaces; - } - - /// - /// Create the validator with default params. - /// - public TextValidatorEnglishCharsOnly() : this(false) - { - } - - /// - /// Return true if text input is only english characters. - /// - /// New text input value. - /// Previous text input value. - /// If TextInput value is legal. - public override bool ValidateText(ref string text, string oldText) - { - return (text.Length == 0 || _regex.IsMatch(text)); - } - } - - /// - /// Make sure input contains only letters, numbers, underscores or hyphens (and optionally spaces). - /// - [System.Serializable] - public class SlugValidator : ITextValidator - { - /// - /// Static ctor. - /// - static SlugValidator() - { - Entity.MakeSerializable(typeof(SlugValidator)); - } - - // the regex to use - System.Text.RegularExpressions.Regex _regex; - - // regex for slug with spaces - static readonly System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_0-9]+$"); - - // regex for slug without spaces - static readonly System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_\ 0-9]+$"); - - // do we allow spaces in text? - private bool _allowSpaces; - - /// - /// Set / get if we allow spaces in text. - /// - public bool AllowSpaces - { - get { return _allowSpaces; } - set { _allowSpaces = value; _regex = _allowSpaces ? _slugWithSpaces : _slugNoSpaces; } - } - - /// - /// Create the slug validator. - /// - /// If true, will allow spaces. - public SlugValidator(bool allowSpaces) - { - AllowSpaces = AllowSpaces; - } - - /// - /// Create the validator with default params. - /// - public SlugValidator() : this(false) - { - } - - /// - /// Return true if text input is slug. - /// - /// New text input value. - /// Previous text input value. - /// If TextInput value is legal. - public override bool ValidateText(ref string text, string oldText) - { - return (text.Length == 0 || _regex.IsMatch(text)); - } - } - - - /// - /// Make sure input don't contain double spaces or tabs. - /// - [System.Serializable] - public class OnlySingleSpaces : ITextValidator - { - /// - /// Static ctor. - /// - static OnlySingleSpaces() - { - Entity.MakeSerializable(typeof(OnlySingleSpaces)); - } - - /// - /// Return true if text input don't contain double spaces or tabs. - /// - /// New text input value. - /// Previous text input value. - /// If TextInput value is legal. - public override bool ValidateText(ref string text, string oldText) - { - return !text.Contains(" ") && !text.Contains("\t"); - } - } - - /// - /// Make sure input is always title, eg starts with a capital letter followed by lowercase. - /// - [System.Serializable] - public class TextValidatorMakeTitle : ITextValidator - { - /// - /// Static ctor. - /// - static TextValidatorMakeTitle() - { - Entity.MakeSerializable(typeof(TextValidatorMakeTitle)); - } - - /// - /// Always return true, and make first character uppercase while all following - /// chars lowercase. - /// - /// New text input value. - /// Previous text input value. - /// Always return true. - public override bool ValidateText(ref string text, string oldText) - { - if (text.Length > 0) - { - text = text.ToLower(); - text = text[0].ToString().ToUpper() + text.Remove(0, 1); - } - return true; - } - } -} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/EnglishCharactersOnly.cs b/GeonBit.UI/Source/Entities/TextValidators/EnglishCharactersOnly.cs new file mode 100644 index 0000000..0e42892 --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/EnglishCharactersOnly.cs @@ -0,0 +1,76 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// Make sure input contains only english characters. + /// + [System.Serializable] + public class EnglishCharactersOnly : ITextValidator + { + /// + /// Static ctor. + /// + static EnglishCharactersOnly() + { + Entity.MakeSerializable(typeof(EnglishCharactersOnly)); + } + + // the regex to use + System.Text.RegularExpressions.Regex _regex; + + // regex for english only with spaces + static readonly System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|]+$"); + + // regex for english only without spaces + static readonly System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|\ ]+$"); + + // do we allow spaces in text? + private bool _allowSpaces; + + /// + /// Set / get if we allow spaces in text. + /// + public bool AllowSpaces + { + get { return _allowSpaces; } + set { _allowSpaces = value; _regex = _allowSpaces ? _slugWithSpaces : _slugNoSpaces; } + } + + /// + /// Create the validator. + /// + /// If true, will allow spaces. + public EnglishCharactersOnly(bool allowSpaces) + { + AllowSpaces = allowSpaces; + } + + /// + /// Create the validator with default params. + /// + public EnglishCharactersOnly() : this(false) + { + } + + /// + /// Return true if text input is only english characters. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public override bool ValidateText(ref string text, string oldText) + { + return (text.Length == 0 || _regex.IsMatch(text)); + } + } +} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/FilenameValidator.cs b/GeonBit.UI/Source/Entities/TextValidators/FilenameValidator.cs new file mode 100644 index 0000000..f4965a5 --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/FilenameValidator.cs @@ -0,0 +1,76 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// Make sure input is a valid filename. + /// + [System.Serializable] + public class FilenameValidator : ITextValidator + { + /// + /// Static ctor. + /// + static FilenameValidator() + { + Entity.MakeSerializable(typeof(SlugValidator)); + } + + // the regex to use + System.Text.RegularExpressions.Regex _regex; + + // regex for slug with spaces + static readonly System.Text.RegularExpressions.Regex _filenameNoSpaces = new System.Text.RegularExpressions.Regex(@"^[^\\/:\*\?""<>\| ]+$"); + + // regex for slug without spaces + static readonly System.Text.RegularExpressions.Regex _filenameWithSpaces = new System.Text.RegularExpressions.Regex(@"^[^\\/:\*\?""<>\|]+$"); + + // do we allow spaces in text? + private bool _allowSpaces; + + /// + /// Set / get if we allow spaces in text. + /// + public bool AllowSpaces + { + get { return _allowSpaces; } + set { _allowSpaces = value; _regex = _allowSpaces ? _filenameWithSpaces : _filenameNoSpaces; } + } + + /// + /// Create the filename validator. + /// + /// If true, will allow spaces. + public FilenameValidator(bool allowSpaces) + { + AllowSpaces = AllowSpaces; + } + + /// + /// Create the validator with default params. + /// + public FilenameValidator() : this(false) + { + } + + /// + /// Return true if text input is slug. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public override bool ValidateText(ref string text, string oldText) + { + return (text.Length == 0 || _regex.IsMatch(text)); + } + } +} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/ITextValidator.cs b/GeonBit.UI/Source/Entities/TextValidators/ITextValidator.cs new file mode 100644 index 0000000..edd988b --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/ITextValidator.cs @@ -0,0 +1,38 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// GeonBit.UI.Entities.TextValidators contains different text validators and processors you can attach to TextInput entities. + /// + [System.Runtime.CompilerServices.CompilerGenerated] + class NamespaceDoc + { + } + + /// + /// A class that validates text input to make sure its valid. + /// These classes can be added to any TextInput to limit the type of input the user can enter. + /// Note: this cannot be an interface due to serialization. + /// + public partial class ITextValidator + { + /// + /// Get the new text input value and return true if valid. + /// This function can either return false to scrap input changes, or change the text and return true. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public virtual bool ValidateText(ref string text, string oldText) { return true; } + } +} diff --git a/GeonBit.UI/Source/Entities/TextValidators/MakeTitleCase.cs b/GeonBit.UI/Source/Entities/TextValidators/MakeTitleCase.cs new file mode 100644 index 0000000..28a4d55 --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/MakeTitleCase.cs @@ -0,0 +1,45 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// Make sure input is always title, eg starts with a capital letter followed by lowercase. + /// + [System.Serializable] + public class MakeTitleCase : ITextValidator + { + /// + /// Static ctor. + /// + static MakeTitleCase() + { + Entity.MakeSerializable(typeof(MakeTitleCase)); + } + + /// + /// Always return true, and make first character uppercase while all following + /// chars lowercase. + /// + /// New text input value. + /// Previous text input value. + /// Always return true. + public override bool ValidateText(ref string text, string oldText) + { + if (text.Length > 0) + { + text = text.ToLower(); + text = text[0].ToString().ToUpper() + text.Remove(0, 1); + } + return true; + } + } +} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/NumbersOnly.cs b/GeonBit.UI/Source/Entities/TextValidators/NumbersOnly.cs new file mode 100644 index 0000000..d1452da --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/NumbersOnly.cs @@ -0,0 +1,107 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// Make sure input is numeric and optionally validate min / max values. + /// + [System.Serializable] + public class NumbersOnly : ITextValidator + { + /// + /// Static ctor. + /// + static NumbersOnly() + { + Entity.MakeSerializable(typeof(NumbersOnly)); + } + + /// + /// Do we allow decimal point? + /// + public bool AllowDecimalPoint; + + /// + /// Optional min value. + /// + public double? Min; + + /// + /// Optional max value. + /// + public double? Max; + + /// + /// Create the number validator. + /// + /// If true, will allow decimal point in input. + /// If provided, will force min value. + /// If provided, will force max value. + public NumbersOnly(bool allowDecimal, double? min = null, double? max = null) + { + AllowDecimalPoint = allowDecimal; + Min = min; + Max = max; + } + + /// + /// Create number validator with default params. + /// + public NumbersOnly() : this(false) + { + } + + /// + /// Return true if text input is a valid number. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public override bool ValidateText(ref string text, string oldText) + { + // if string empty return true + if (text.Length == 0) + { + return true; + } + + // will contain value as number + double num; + + // try to parse as double + if (AllowDecimalPoint) + { + if (!double.TryParse(text, out num)) + { + return false; + } + } + // try to parse as int + else + { + int temp; + if (!int.TryParse(text, out temp)) + { + return false; + } + num = temp; + } + + // validate range + if (Min != null && num < (double)Min) { text = Min.ToString(); } + if (Max != null && num > (double)Max) { text = Max.ToString(); } + + // valid number input + return true; + } + } +} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/OnlySingleSpaces.cs b/GeonBit.UI/Source/Entities/TextValidators/OnlySingleSpaces.cs new file mode 100644 index 0000000..7558a9b --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/OnlySingleSpaces.cs @@ -0,0 +1,40 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + + /// + /// Make sure input don't contain double spaces or tabs. + /// + [System.Serializable] + public class OnlySingleSpaces : ITextValidator + { + /// + /// Static ctor. + /// + static OnlySingleSpaces() + { + Entity.MakeSerializable(typeof(OnlySingleSpaces)); + } + + /// + /// Return true if text input don't contain double spaces or tabs. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public override bool ValidateText(ref string text, string oldText) + { + return !text.Contains(" ") && !text.Contains("\t"); + } + } +} \ No newline at end of file diff --git a/GeonBit.UI/Source/Entities/TextValidators/SlugValidator.cs b/GeonBit.UI/Source/Entities/TextValidators/SlugValidator.cs new file mode 100644 index 0000000..3c139be --- /dev/null +++ b/GeonBit.UI/Source/Entities/TextValidators/SlugValidator.cs @@ -0,0 +1,76 @@ +#region File Description +//----------------------------------------------------------------------------- +// Validators you can attach to TextInput entities to manipulate and validate +// user input. These are used to create things like text input for numbers only, +// limit characters to english chars, etc. +// +// Author: Ronen Ness. +// Since: 2016. +//----------------------------------------------------------------------------- +#endregion + +namespace GeonBit.UI.Entities.TextValidators +{ + /// + /// Make sure input contains only letters, numbers, underscores or hyphens (and optionally spaces). + /// + [System.Serializable] + public class SlugValidator : ITextValidator + { + /// + /// Static ctor. + /// + static SlugValidator() + { + Entity.MakeSerializable(typeof(SlugValidator)); + } + + // the regex to use + System.Text.RegularExpressions.Regex _regex; + + // regex for slug with spaces + static readonly System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_0-9]+$"); + + // regex for slug without spaces + static readonly System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_\ 0-9]+$"); + + // do we allow spaces in text? + private bool _allowSpaces; + + /// + /// Set / get if we allow spaces in text. + /// + public bool AllowSpaces + { + get { return _allowSpaces; } + set { _allowSpaces = value; _regex = _allowSpaces ? _slugWithSpaces : _slugNoSpaces; } + } + + /// + /// Create the slug validator. + /// + /// If true, will allow spaces. + public SlugValidator(bool allowSpaces) + { + AllowSpaces = AllowSpaces; + } + + /// + /// Create the validator with default params. + /// + public SlugValidator() : this(false) + { + } + + /// + /// Return true if text input is slug. + /// + /// New text input value. + /// Previous text input value. + /// If TextInput value is legal. + public override bool ValidateText(ref string text, string oldText) + { + return (text.Length == 0 || _regex.IsMatch(text)); + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index fc0a9f5..50b52a5 100644 --- a/README.md +++ b/README.md @@ -2100,6 +2100,9 @@ For older MonoGame versions, see [tag 2.1.0.0](https://github.com/RonenNess/Geon - Changed Slider and Progress bar Min and Max properties to support negative numbers. Now sliders can go negative! - Fixed typos. - Added option to create Image from path. +- Improved Message Boxes utility, and added Buttons list to message box handle. +- Added built-in files dialog (save / load file). +- Renamed and rearranged text validators + added file name validator. ## Credits