File tree Expand file tree Collapse file tree 3 files changed +18
-1
lines changed Expand file tree Collapse file tree 3 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 17
17
"notEqualErrorText": "This field value must not be equal to {value}.",
18
18
"numericErrorText": "Value must be numeric.",
19
19
"requiredErrorText": "This field cannot be empty.",
20
- "urlErrorText": "This field requires a valid URL address."
20
+ "urlErrorText": "This field requires a valid URL address.",
21
+ "phoneErrorText": "This field requires a valid phone number."
21
22
}
Original file line number Diff line number Diff line change @@ -314,4 +314,13 @@ class FormBuilderValidators {
314
314
! isDate (valueCandidate! )
315
315
? errorText ?? FormBuilderLocalizations .current.dateStringErrorText
316
316
: null ;
317
+
318
+ /// [FormFieldValidator] that requires the field's value to be a valid phone number.
319
+ static FormFieldValidator <String > phoneNumber ({
320
+ String ? errorText,
321
+ }) =>
322
+ (valueCandidate) => true == valueCandidate? .isNotEmpty &&
323
+ ! isPhoneNumber (valueCandidate! )
324
+ ? errorText ?? FormBuilderLocalizations .current.phoneErrorText
325
+ : null ;
317
326
}
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ 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}$' );
16
+
15
17
int _maxUrlLength = 2083 ;
16
18
17
19
/// check if the string [str] is an email
@@ -234,3 +236,8 @@ bool isDate(String str) {
234
236
return false ;
235
237
}
236
238
}
239
+
240
+ /// check if the string is a valid phone number
241
+ bool isPhoneNumber (String str) {
242
+ return _phoneNumber.hasMatch (str);
243
+ }
You can’t perform that action at this time.
0 commit comments