Android edittext - do not allow decimal separator alone
I have an Edittext, where I want to allow only numbers and decimal numbers (max 2 decimals after separator e.g. 125.50).
I implemented a filter for this:
final EditText field1 = (EditText)findViewById(R.id.field1);
field1.setFilters(new InputFilter { filter });
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=5;
final int maxDigitsAfterDecimalPoint=2;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([0-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
This is working fine, but the problem is, that if user inserts decimal separator ALONE, I've got java.lang.NumberFormatException: For input string: "."
In my ontextChanged I tried this:
if(field1.getText().toString().equals("[.]")){field1.setText(0);}
also this
if(field1.getText().toString().equals(".")){field1.setText(0);}
but did not work.
How can I restrict the decimal separator alone, but allow it with the numbers?


add a comment |
I have an Edittext, where I want to allow only numbers and decimal numbers (max 2 decimals after separator e.g. 125.50).
I implemented a filter for this:
final EditText field1 = (EditText)findViewById(R.id.field1);
field1.setFilters(new InputFilter { filter });
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=5;
final int maxDigitsAfterDecimalPoint=2;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([0-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
This is working fine, but the problem is, that if user inserts decimal separator ALONE, I've got java.lang.NumberFormatException: For input string: "."
In my ontextChanged I tried this:
if(field1.getText().toString().equals("[.]")){field1.setText(0);}
also this
if(field1.getText().toString().equals(".")){field1.setText(0);}
but did not work.
How can I restrict the decimal separator alone, but allow it with the numbers?


try this^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
– Karan Mer
Nov 21 '18 at 9:16
add a comment |
I have an Edittext, where I want to allow only numbers and decimal numbers (max 2 decimals after separator e.g. 125.50).
I implemented a filter for this:
final EditText field1 = (EditText)findViewById(R.id.field1);
field1.setFilters(new InputFilter { filter });
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=5;
final int maxDigitsAfterDecimalPoint=2;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([0-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
This is working fine, but the problem is, that if user inserts decimal separator ALONE, I've got java.lang.NumberFormatException: For input string: "."
In my ontextChanged I tried this:
if(field1.getText().toString().equals("[.]")){field1.setText(0);}
also this
if(field1.getText().toString().equals(".")){field1.setText(0);}
but did not work.
How can I restrict the decimal separator alone, but allow it with the numbers?


I have an Edittext, where I want to allow only numbers and decimal numbers (max 2 decimals after separator e.g. 125.50).
I implemented a filter for this:
final EditText field1 = (EditText)findViewById(R.id.field1);
field1.setFilters(new InputFilter { filter });
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=5;
final int maxDigitsAfterDecimalPoint=2;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([0-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
This is working fine, but the problem is, that if user inserts decimal separator ALONE, I've got java.lang.NumberFormatException: For input string: "."
In my ontextChanged I tried this:
if(field1.getText().toString().equals("[.]")){field1.setText(0);}
also this
if(field1.getText().toString().equals(".")){field1.setText(0);}
but did not work.
How can I restrict the decimal separator alone, but allow it with the numbers?




asked Nov 20 '18 at 21:32
wotanwotan
17910
17910
try this^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
– Karan Mer
Nov 21 '18 at 9:16
add a comment |
try this^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
– Karan Mer
Nov 21 '18 at 9:16
try this
^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:– Karan Mer
Nov 21 '18 at 9:16
try this
^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:– Karan Mer
Nov 21 '18 at 9:16
add a comment |
2 Answers
2
active
oldest
votes
If this:
if(field1.getText().toString().equals(".")){field1.setText(0);}
is your exact code, then for sure it failed because 0
is not a string, it's an integer considered to be a resource id.
So first try this:
if(field1.getText().toString().equals(".")){field1.setText("0");}
If it fails again then consider that a NumberFormatException
can be resolved by try/catch
like this:
double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
add a comment |
For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176
For other references, if you want using want to try using textWatcher:
https://stackoverflow.com/a/16684661/10396176
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401895%2fandroid-edittext-do-not-allow-decimal-separator-alone%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If this:
if(field1.getText().toString().equals(".")){field1.setText(0);}
is your exact code, then for sure it failed because 0
is not a string, it's an integer considered to be a resource id.
So first try this:
if(field1.getText().toString().equals(".")){field1.setText("0");}
If it fails again then consider that a NumberFormatException
can be resolved by try/catch
like this:
double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
add a comment |
If this:
if(field1.getText().toString().equals(".")){field1.setText(0);}
is your exact code, then for sure it failed because 0
is not a string, it's an integer considered to be a resource id.
So first try this:
if(field1.getText().toString().equals(".")){field1.setText("0");}
If it fails again then consider that a NumberFormatException
can be resolved by try/catch
like this:
double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
add a comment |
If this:
if(field1.getText().toString().equals(".")){field1.setText(0);}
is your exact code, then for sure it failed because 0
is not a string, it's an integer considered to be a resource id.
So first try this:
if(field1.getText().toString().equals(".")){field1.setText("0");}
If it fails again then consider that a NumberFormatException
can be resolved by try/catch
like this:
double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}
If this:
if(field1.getText().toString().equals(".")){field1.setText(0);}
is your exact code, then for sure it failed because 0
is not a string, it's an integer considered to be a resource id.
So first try this:
if(field1.getText().toString().equals(".")){field1.setText("0");}
If it fails again then consider that a NumberFormatException
can be resolved by try/catch
like this:
double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}
answered Nov 20 '18 at 21:52
forpasforpas
11.6k3424
11.6k3424
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
add a comment |
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
perfect, the .setText("0") fixed it. Thank you!
– wotan
Nov 21 '18 at 18:27
add a comment |
For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176
For other references, if you want using want to try using textWatcher:
https://stackoverflow.com/a/16684661/10396176
add a comment |
For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176
For other references, if you want using want to try using textWatcher:
https://stackoverflow.com/a/16684661/10396176
add a comment |
For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176
For other references, if you want using want to try using textWatcher:
https://stackoverflow.com/a/16684661/10396176
For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176
For other references, if you want using want to try using textWatcher:
https://stackoverflow.com/a/16684661/10396176
answered Nov 21 '18 at 8:59


JEFFJEFF
144
144
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401895%2fandroid-edittext-do-not-allow-decimal-separator-alone%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
try this
^[1-9]d*(.d+)?$
regex it allows numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:– Karan Mer
Nov 21 '18 at 9:16