Skip to content

Instantly share code, notes, and snippets.

@bienvenuelisis
Last active March 7, 2022 15:03
Show Gist options
  • Save bienvenuelisis/c63a1f66b43a851eb4639a5b51897b0e to your computer and use it in GitHub Desktop.
Save bienvenuelisis/c63a1f66b43a851eb4639a5b51897b0e to your computer and use it in GitHub Desktop.
String? validateIntValue(
String value,
String errorMsg, {
bool positive = false,
int maxValue = 9223372036854775807,
}) {
if (value.isEmpty) {
return errorMsg;
} else {
try {
int i = int.parse(value);
if (i > maxValue) {
return errorMsg;
}
if (positive && i < 0) {
return errorMsg;
}
return null;
} catch (e) {
return errorMsg;
}
}
}
String? validateDoubleValue(
String value,
String errorMsg, {
bool positive = false,
double? maxValue = double.maxFinite,
}) {
if (value.isEmpty) {
return errorMsg;
} else {
try {
value = value.replaceFirst(",", ".");
double d = double.parse(value);
if (maxValue != null && d > maxValue) {
return errorMsg;
}
if (positive && d < 0) {
return errorMsg;
}
return null;
} catch (e) {
return errorMsg;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment