InputFilter Pattern isn't allowing decimal - android

I want a user to be able to input numbers such as the following.
Valid:
~0
~0.00
~12.34
~301.7
~4
Invalid
~01
~3.001
In short, it allows decimal numbers up to two decimal places.
This is what I've been trying to use
Pattern mPattern = Pattern.compile("|(0|[1-9]+[0-9]*)(\\.[0-9]{1,2})?");
When I try to type a "." in the field, it won't let me.

I think the problem is that your validation pattern needs to match the input as you are entering it. In your case, as soon as you type in the ".", your entry is invalid. For example, if you are trying to enter 1.23, when you are entering the decimal point your entry becomes 1., which does not match your regexp.
Try replacing {1,2} with {0,1,2} in your expression to allow a trailing ".".

Related

Regex with numbers and alphabets mandatory, special characters optional

In my application edittext value need at least one digit and one alphabet is mandatory, and some special characters are optional i.e ".-", like any whare in the string.
For example ram123-. or r_m-12.m or .--ram123 or ram123.-_.
For this I need regex. I have already tried with this one
str_userId.matches("[A-Za-z0-9]*+[?.?_?-]")
But not working. Here how to add special characters are optional.
Thanks, In Advance
You could use a positive lookahead (?= to assert at least one occurrence of a-z and after that match at least a single digit [0-9].
Before and after matching the digit, you could add the . _ and - to the character class [A-Za-z._-]* and repeat it 0+ times.
Note that a character class matches on of the listed characters. This notation [?.?_?-], which could be written as [?._-] would also match a question mark instead of making the others optional
^(?=[^a-z\n]*[a-z])[A-Za-z._-]*[0-9][A-Za-z0-9._-]*$
Regex demo

How to implement regex for userID field?

I have one userID edittext field, here I am allowing alphabets and numbers and '.' and '_' and '#' special characters. But how to restrict only '.' and '_' and '#' in edittext field? I want to restrict like this:
After text only I want to allow those special characters.
Only one time I want to allow those spl characters(don't allow two times like ##,..,__ like this)
end of the userID no spl characters.
test.new_bike sample example for valid.
test..new__bike,........,______,###### like words I won't allow.
please help me,this is my regex
^(([A-Za-z0-9]+\\s{1}[A-Za-z0-9]+)|([A-Za-z0-9._]+))$
Try this one
"^[A-Za-z0-9]+[\\.\\#\\_]{0,1}[A-Za-z0-9]+[\\.\\#\\_]{0,1}[A-Za-z0-9]+[\\.\\#\\_]{0,1}[A-Za-z0-9]+$"

format edittext input price format

When we work with the currency we need to use ',' separator in appropriate places . For example 1500 as 1,500.
I have a issue. My applications require formatting the EditText's value while typing. I.E., a number that needs to be formatted with decimal and thousands separators. Example, I input 123456789, the EditText display 123.456.789.
How to I do this issue ? Thank all.

Android Regular Expression - Replace all spaces '(' ')' '-'

I newt o regular expressions and been using tutorials, but the regular express I have works sometimes, but doesn't all the time. I am getting my numbers out of the contact list from my android phone. I am trying to get rid of all spaces, '(', ')', and '-'
For example:
1. (555) 867-5309 -> 5558675309
2. 1555-555-5555 -> 15555555555
3. 555-555-5555 -> 5555555555
This is the line I am using
String formatphone = contactPhone.replaceAll("\\s()-","");
For some numbers it only returns number and sometimes it doesn't change the format.
Is it correct? Do i need to format something because I am taking it out of the phone's contact list?
Put the desired characters in a character class:
String formatphone = contactPhone.replaceAll("[ ()-]","");
Ensure that you put the hyphen - at either end.
Try using this:
String formatphone = contactPhone.replaceAll("^.*[\\s\\(\\)-].*", "");
As a regular expression you're defining a set using []. In that set you include any character you want to be replaced. As ( and ) are special meaning characters, you have to escape them. As the - is a special character used to design ranges, it has to be the last character of your set, so if nothing is behind it, it's not a range, but just that character (you could escape it too, though).

Android validating EditText decimal point "."

I am doing some math on the values of two EditText fields, I want to have the following validations on them:
They are not empty.
They are valid to each other(if the first field was integer the second should be the same, if the first was decimal the second should be the same).
I cannot figure out how to validate the decimal values or specifically the decimal point.
I have tried this out, but it didn't work. My app just crashes.
if (editText1.getText().toString().equals(".") || editText2.getText().toString().equals("."))
return;
For decimal value you can use this:
EditText et = (EditText) findViewById(R.id.myTextView) ;
et.setInputType(0x00002002);
It will accept only decimal values.
Another solution is here.
You can use contains() method to check if your string contains the argument you provided
if (textView1.getText().toString().contains(".") || textView2.getText().toString().contains(".")) {
Toast.makeText(getApplicationContext(), "Wrong Values", Toast.LENGTH_LONG).show();
First of all, you want to set your input type to accept decimals and/or numbers.
You can set it up in the xml layout:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|number"
/>
Then you can just convert your EditText text to double:
double result = new Double(editText1.getText().toString());
or
double result = Double.valueOf(editText1.getText().toString());
This rocket science that you wroted is not needed anymore after that.
You can test whether something is a valid value or not by trying to parse it with Float.parseFloat and seeing if it throws a NumberFormatException (via try/catch). If it does, then you know the input is invalid.
Btw, it's nice if you provide a visual cue to the user to inform them that the input is invalid. As opposed to waiting until the user presses the "calculate" button and only tell them then.

Categories

Resources