Im taking day month and year in 3 Textfields but none of the DateTime property or function is providing the validation.
I have used DateTime.parse(str) to get the date but it adds the remaining months to the year if month field is entered with more then 12 same for days too.
bool isDate(String str) {
try {
DateTime.parse(str);
return true;
} catch (e) {
return false;
}
}
I want if some one enters '20053050' to the string to be false not true. But this functions always return true !!!
enter image description here
In this example, learn how to add validation to a form that has a single text field using the following steps: Create a Form with a GlobalKey . Add a TextFormField with validation logic. Create a button to validate and submit the form.
isUUID - check if the string is a UUID (version 3, 4 or 5). matches - check if string str matches the Regex pattern. In this way, you can validate TextFormFiled or TextField data with Dart Regex and package in Flutter App.
TextFormField is also a better option for validation. Flutter handles error text itself, so we don’t require to use variable _validate. It will check at runtime whether you satisfied the condition or not. Note: The user must add at least one character to get this error message. Using a TextFormField with a validator.
if(fname.text.length > 10){ //length of textfield with controller "name" has more than 10 characters. } Furthermore, you can validate data easily without using Regex. For that, add validators flutter packag e to your dependency by adding the following line in pubspec.yaml file: contains - check if the string contains the seed.
_formKey is used as a global identifier to our form that we will use when submitting and validating the form _dateController is a TextEditingController that we use to manually change the text inside the date text field that we are going to create selectedDate is used to store the date that is returned from the date picker
There's simple method you can try now. Which is tryParse method. It simply returns null for invalid date format.
if(DateTime.tryParse(timeString) != null){
DateTime date = DateTime.parse(timeString);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With