I can do something like this:
double.parse(myString)
That's fine when I have "1.1" but if I have "1.1.1" it fails with Invalid double. Could I detect this somehow in advance?
I'd need this for input validations
Working with
double.tryParse(myString)
As written above, use double.tryParse() or wrap double.parse() with try catch
An alternative, if you need to do input validation could be filter away "bad" input already when the user inputs the number.
You could change keyboard type and use input formatters on a TextField / TextFormFiel
TextField(
decoration: new InputDecoration(labelText: "Enter your number"),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter> [
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
),
You could write something like this
if(!myString.contains(<someregex>)) {
double.parse(myString);
}
Where the regex is validating that the string is a valid double, the value of doing this over using tryParse is that you can include your business rule validation alongside your datatype validation.
Related
I am developing an application with Flutter. I put TextFormField. I want to prevent entering numbers in this TextFormField. How can I do that?
You can use the validator argument to create a callback if the user input a number.
You can also add the keyboardType argument to limit the user to only text with the value TextInputType.text
Try this:
TextFormField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.deny(RegExp('[0-9]')),
],
),
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("[0-9]")),
],
You need to add this
I am trying to make a otp field which I can fill manually and validate later. I am using the Pinput package in flutter as it is quite popular. I am facing following problems-
The pin does not start typing unless I enter a . / , in the first field
The Pinput() fields are cleared as soon as I enter the last digit.
How to use this pin and validate it later?
My code is-
Pinput(
length: 6,
keyboardType: TextInputType.number,
controller: pinController,
defaultPinTheme: defaultPinTheme,
focusedPinTheme: focusedPinTheme,
submittedPinTheme: submittedPinTheme,
pinputAutovalidateMode: null,
textInputAction: TextInputAction.next,
showCursor: true,
validator: (s) {
print('validating code: $s');
},
onCompleted: null,
),
Please help!!
Use onCompleted like this not null
onCompleted: (pin) => print(pin),
This onCompleted method do like if entering input got finish do something like navigation or check thing
When you entered last digit this will print
You need to use regex to validate just search and get what you need
// In validator you can check
Int validDigit = 1234; // this is test digit
Validator: (input){
return s == validDigit ? null : 'Pin is incorrect';
}
Validator works like if your input digit was not like the pattern you need, in this case if the input digit was not equal to the code that you send to user, you need to return error otherwise return null it means code is correct.
in above e.g the validDigit comes from api or somewhere else
And finally set auto validate like here not null:
pinputAutovalidateMode: PinputAutovalidateMode.onSubmit,
I hope it works, sorry they are not in order of your question.
I'm trying to use the inputFormatters attribute to restrict input to a decimal number. The way to do that seems to be a regex string, however, it doesn't allow any input for some reason. If I just have a dot as regular expression, it works fine, but there's something about the complexity of the string that breaks functionality. Here's my code:
TextField(
keyboardType: TextInputType.number,
maxLines: 1,
maxLength: 8,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("/^(0|[1-9]\\d*)(\\.\\d+)?\$/"))],
onChanged: (value) => setState(() {
valoare = value;
})
),
The problem is the inputFormatters: are apparently running on each keystroke, even on partial input (input so far).
So to enter 123.45 (which is valid), you must go through a step that where the value is "123.", but that's invalid, and now you're toast. Move your validation instead to the "validate:" method, which is called only when the input is complete.
you can use options with keyboard type
like
keyboardType: TextInputType.numberWithOptions(decimal: true,signed: false),
I've got Text field which updateds a value variable to double format in onChange... I want the TextField only display the updated the value variable.. I don't want the textfield to display any string value or values which are not in double format by pressing wrong keyboard key. Even with numeric keyboard with decimal enabled we can press decimal key multiple times which I don't want to display in the TextField.
TextFormField(
decoration: InputDecoration(labelText: 'Value'),
onChanged: (s) {
if (double.tryParse(s) != null)
setState(() {
value = double.parse(s);
});
},
)
How can I display only parsed value in TextField?
Please add below attribute to TextFormField Widget
TextFormField(
keyboardType:TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
],
);
#user8773560 was correct however RegExp for decimal number was wrong and not working so the correct answer is
inputFormatters: [
WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)'))
]
Also answered in my other question Regex for double numbers in dart
Answer based on #delmin's answer, but allows negative numbers as well.
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)'))
]
Hello you can not use onChange for this purpose because its called only once TextField has changed.
You should try this.
TextFormField(
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(12), //max length of 12 characters
WhitelistingTextInputFormatter.digitsOnly,//Only numbers
BlacklistingTextInputFormatter.singleLineFormatter,//No line break
WhitelistingTextInputFormatter(RegExp("[0-9]+.[0-9]")) //only double values
],
)
Lear more here or Official doc
So i am making a chat app and i want user to submit username which should not be uppercase and have space, i can accept the name and change it later in onchanged feature but i want user to know it as well
Flutter's TextField or TextFormField has attribute named inputFormatters which takes a list of TextInputFormatter.
an example of TextInputFormatters that is useful in your case.
FilteringTextInputFormatter.allow(RegExp("[a-z]"))
TextField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("[a-z]")),
],
)
you can see TextInputFormatters API docs here : Reference
(before flutter 1.20):
WhitelistingTextInputFormatter(RegExp("[a-z]")),
TextField(
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter(RegExp("[a-z]")),
],
)
you can take those as reference if it's not clear enough : Reference 2, Reference 3.
Also check out this SOF Question: Reference 4
In regards to changing the text, try doing the following:
Let's say 's' is the username:
String s = ""
onChange(val) {
s = val.trim().toLowerCase()
}
If you want to notify the user, perhaps use an alert dialog with some text letting them know the username should not be uppercase and contain no spaces. Regardless, you can't assume the user will conform to what they "should do".
You can use a Form and then notify the user using the error field (Validator)
TextFormField(
validator: (name) {
Pattern pattern = r'^[a-z]+$'; // Regex for lowercase only
RegExp regex = new RegExp(pattern);
if (!regex.hasMatch(name))
return 'Username must be lowercase, this will be changed when saved';
else
return null;
},
),
FlatButton(
child: Text('Save'),
onPressed: () {
_formKey.currentState.validate(); // just check if its valid and notify user
// Other code to save the and change the value
print('Saving Username');
},
)
Dart Pad Example