@@ -12,10 +12,17 @@ RegExp _creditCard = RegExp(
12
12
r'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$' ,
13
13
);
14
14
15
- RegExp _phoneNumber = RegExp (r'^(\+?\d{0,1})?\(?\d{3}\)?[-.\s]\d{3}[-.\s]\d{4}$' );
15
+ RegExp _phoneNumber =
16
+ RegExp (r'^(\+?\d{0,1})?\(?\d{3}\)?[-.\s]\d{3}[-.\s]\d{4}$' );
16
17
17
18
RegExp _creditCardExpirationDate = RegExp (r'^[0-1][0-9]/\d{2}$' );
18
19
20
+ RegExp _hexRegExp = RegExp (r'^#[0-9a-fA-F]{6}$' );
21
+
22
+ RegExp _rgbRegExp = RegExp (r'^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$' );
23
+
24
+ RegExp _hslRegExp = RegExp (r'^hsl\(\d+,\s*\d+%,\s*\d+%\)$' );
25
+
19
26
int _maxUrlLength = 2083 ;
20
27
21
28
/// check if the string [str] is an email
@@ -284,3 +291,40 @@ bool isNotExpiredCreditCardDate(String str) {
284
291
285
292
return true ;
286
293
}
294
+
295
+ bool isColorCode (String value,
296
+ {List <String > formats = const ['hex' , 'rgb' , 'hsl' ]}) {
297
+ if (formats.contains ('hex' )) {
298
+ if (_hexRegExp.hasMatch (value)) return true ;
299
+ }
300
+ if (formats.contains ('rgb' )) {
301
+ if (_rgbRegExp.hasMatch (value)) {
302
+ final parts = value.substring (4 , value.length - 1 ).split (',' );
303
+ for (final part in parts) {
304
+ final int colorValue = int .tryParse (part.trim ()) ?? - 1 ;
305
+ if (colorValue < 0 || colorValue > 255 ) {
306
+ return false ;
307
+ }
308
+ }
309
+ return true ;
310
+ }
311
+ }
312
+ if (formats.contains ('hsl' )) {
313
+ if (_hslRegExp.hasMatch (value)) {
314
+ final parts = value.substring (4 , value.length - 1 ).split (',' );
315
+ for (var i = 0 ; i < parts.length; i++ ) {
316
+ final int colorValue = int .tryParse (parts[i].trim ()) ?? - 1 ;
317
+ if (i == 0 ) {
318
+ // Hue
319
+ if (colorValue < 0 || colorValue > 360 ) {
320
+ return false ;
321
+ }
322
+ } else if (colorValue < 0 || colorValue > 100 ) {
323
+ return false ;
324
+ }
325
+ }
326
+ return true ;
327
+ }
328
+ }
329
+ return false ;
330
+ }
0 commit comments