Skip to content

Commit adca581

Browse files
committed
changed version to 3.0.1.3 and added / improved text validators
1 parent 839e1a8 commit adca581

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

GeonBit.UI/GeonBitUI_Examples.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,14 +1034,16 @@ Note that in order to use clipping and scrollbar with Panels you need to set the
10341034
// first name
10351035
TextInput firstName = new TextInput(false, new Vector2(0.4f, -1), anchor: Anchor.Auto);
10361036
firstName.PlaceholderText = "Name";
1037-
firstName.Validators.Add(new TextValidatorEnglishCharsOnly());
1037+
firstName.Validators.Add(new TextValidatorEnglishCharsOnly(true));
1038+
firstName.Validators.Add(new OnlySingleSpaces());
10381039
firstName.Validators.Add(new TextValidatorMakeTitle());
10391040
entitiesGroup.AddChild(firstName);
10401041

10411042
// last name
10421043
TextInput lastName = new TextInput(false, new Vector2(0.4f, -1), anchor: Anchor.AutoInline);
10431044
lastName.PlaceholderText = "Surname";
1044-
lastName.Validators.Add(new TextValidatorEnglishCharsOnly());
1045+
lastName.Validators.Add(new TextValidatorEnglishCharsOnly(true));
1046+
lastName.Validators.Add(new OnlySingleSpaces());
10451047
lastName.Validators.Add(new TextValidatorMakeTitle());
10461048
entitiesGroup.AddChild(lastName);
10471049

GeonBit.UI/Source/Entities/TextInputValidators.cs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,87 @@ public bool ValidateText(ref string text, string oldText)
106106
/// Make sure input contains only english characters.
107107
/// </summary>
108108
public class TextValidatorEnglishCharsOnly : ITextValidator
109+
{
110+
// the regex to use
111+
System.Text.RegularExpressions.Regex _regex;
112+
113+
// regex for english only with spaces
114+
static System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|]+$");
115+
116+
// regex for english only without spaces
117+
static System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z|\ ]+$");
118+
119+
/// <summary>
120+
/// Create the validator.
121+
/// </summary>
122+
/// <param name="allowSpaces">If true, will allow spaces.</param>
123+
public TextValidatorEnglishCharsOnly(bool allowSpaces = false)
124+
{
125+
_regex = allowSpaces ? _slugWithSpaces : _slugNoSpaces;
126+
}
127+
128+
/// <summary>
129+
/// Return true if text input is only english characters.
130+
/// </summary>
131+
/// <param name="text">New text input value.</param>
132+
/// <param name="oldText">Previous text input value.</param>
133+
/// <returns>If TextInput value is legal.</returns>
134+
public bool ValidateText(ref string text, string oldText)
135+
{
136+
return (text.Length == 0 || _regex.IsMatch(text));
137+
}
138+
}
139+
140+
/// <summary>
141+
/// Make sure input contains only letters, numbers, underscores or hyphens (and optionally spaces).
142+
/// </summary>
143+
public class SlugValidator : ITextValidator
144+
{
145+
// the regex to use
146+
System.Text.RegularExpressions.Regex _regex;
147+
148+
// regex for slug with spaces
149+
static System.Text.RegularExpressions.Regex _slugNoSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_]+$");
150+
151+
// regex for slug without spaces
152+
static System.Text.RegularExpressions.Regex _slugWithSpaces = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z\-_\ ]+$");
153+
154+
/// <summary>
155+
/// Create the slug validator.
156+
/// </summary>
157+
/// <param name="allowSpaces">If true, will allow spaces.</param>
158+
public SlugValidator(bool allowSpaces = false)
159+
{
160+
_regex = allowSpaces ? _slugWithSpaces : _slugNoSpaces;
161+
}
162+
163+
/// <summary>
164+
/// Return true if text input is slug.
165+
/// </summary>
166+
/// <param name="text">New text input value.</param>
167+
/// <param name="oldText">Previous text input value.</param>
168+
/// <returns>If TextInput value is legal.</returns>
169+
public bool ValidateText(ref string text, string oldText)
170+
{
171+
return (text.Length == 0 || _regex.IsMatch(text));
172+
}
173+
}
174+
175+
176+
/// <summary>
177+
/// Make sure input don't contain double spaces or tabs.
178+
/// </summary>
179+
public class OnlySingleSpaces : ITextValidator
109180
{
110181
/// <summary>
111-
/// Return true if text input is only english characters
182+
/// Return true if text input don't contain double spaces or tabs.
112183
/// </summary>
113184
/// <param name="text">New text input value.</param>
114185
/// <param name="oldText">Previous text input value.</param>
115186
/// <returns>If TextInput value is legal.</returns>
116187
public bool ValidateText(ref string text, string oldText)
117188
{
118-
return (text.Length == 0 || System.Text.RegularExpressions.Regex.IsMatch(text, @"^[a-zA-Z|]+$"));
189+
return !text.Contains(" ") && !text.Contains("\t");
119190
}
120191
}
121192

GeonBit.UI/Source/UserInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public enum BuiltinThemes
8585
public class UserInterface
8686
{
8787
/// <summary>Current GeonBit.UI version identifier.</summary>
88-
public const string VERSION = "3.0.1.2";
88+
public const string VERSION = "3.0.1.3";
8989

9090
/// <summary>
9191
/// The currently active user interface instance.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,12 @@ For older MonoGame versions, see [tag 2.1.0.0](https://github.com/RonenNess/Geon
17701770
- Fixed combobox and radio buttons label positioning with global scaling.
17711771
- Fixed mouse position on first frame of a new User Interface.
17721772

1773+
## 3.0.1.3
1774+
1775+
- Added outline opacity property.
1776+
- Added some text validators.
1777+
- Improved existing text validators efficiency + added support in spaces / no spaces.
1778+
17731779
## Credits
17741780

17751781
GeonBit.UI was written by Ronen Ness, but uses some free textures made by awesome people who share their work for free.

0 commit comments

Comments
 (0)