@@ -106,16 +106,87 @@ public bool ValidateText(ref string text, string oldText)
106
106
/// Make sure input contains only english characters.
107
107
/// </summary>
108
108
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
109
180
{
110
181
/// <summary>
111
- /// Return true if text input is only english characters
182
+ /// Return true if text input don't contain double spaces or tabs.
112
183
/// </summary>
113
184
/// <param name="text">New text input value.</param>
114
185
/// <param name="oldText">Previous text input value.</param>
115
186
/// <returns>If TextInput value is legal.</returns>
116
187
public bool ValidateText ( ref string text , string oldText )
117
188
{
118
- return ( text . Length == 0 || System . Text . RegularExpressions . Regex . IsMatch ( text , @"^[a-zA-Z|]+$" ) ) ;
189
+ return ! text . Contains ( " " ) && ! text . Contains ( " \t " ) ;
119
190
}
120
191
}
121
192
0 commit comments