I have a textfield and i want to ensure the data or entry is not more than the intended value.example "the value to be entered should not be more than $4000".
i did some research but i still have been able to solve the problem.
`
TextFormField(
decoration: InputDecoration(
hintStyle: TextStyle(
fontFamily: "Proxima Nova",
fontWeight: FontWeight.w300,
),
border: InputBorder.none,
labelStyle: TextStyle(
color: Color(0xffFAFAFA),
),
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r"[0-9] |\s"))
],
controller: kiloMeter,
validator: (value) {
if (value != null &&
value.isEmpty &&
value.length < 4) {
return 'Please enter the price you want to purchase';
}
return null;
},
)
: const SizedBox(
height: 120,
);
`
CodePudding user response:
Try the following code:
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintStyle: TextStyle(
fontFamily: "Proxima Nova",
fontWeight: FontWeight.w300,
),
border: InputBorder.none,
labelStyle: TextStyle(
color: Color(0xffFAFAFA),
),
),
controller: kiloMeter,
validator: (value) {
if (value != null && value.isEmpty && int.parse(value) > 4000) {
return 'Please enter the price you want to purchase';
}
return null;
},
)
: const SizedBox(
height: 120,
);
CodePudding user response:
You have to use parsing in the validator to check the value
You can easily convert the string value that comes from the TextFormField to int by use int.parse(value)
.
So your code would be:
TextFormField(
decoration: InputDecoration(
hintStyle: TextStyle(
fontFamily: "Proxima Nova",
fontWeight: FontWeight.w300,
),
border: InputBorder.none,
labelStyle: TextStyle(
color: Color(0xffFAFAFA),
),
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r"[0-9] |\s"))
],
controller: kiloMeter,
validator: (value) {
if (value != null &&
value.isEmpty &&
int.parse(value) <= 4000 {
return 'Please enter the price you want to purchase';
}
return null;
},
)
: const SizedBox(
height: 120,
);