Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:34:30+00:00 2026-06-02T17:34:30+00:00

I have a numberDecimal EditText which I want to validate using a regular expression.

  • 0

I have a numberDecimal EditText which I want to validate using a regular expression. In validation what I want is:

  1. Before the decimal point, the maximum digit I want to enter is three and the digit should not start with zero like 2,23,342, etc.

  2. After the decimal point, the maximum digit I want to enter is one like .1, .3, .6, etc.

So the number that I allow the user to enter is like 2.1, 32.5, 444.8, 564.9, etc.

But in my code, what happens is:

  1. It allows the user to enter more than a three digit number before the decimal point like 3456, 4444, 5555 and after that it doesn’t allow me to enter a decimal point after that.

  2. It allows me to enter 0 before the decimal point as the start of the digit.

So why does this happen, is anything wrong in the regular expression I have used? If anyone knows, please help me to solve this.

Code I have used:

weightEditText.addTextChangedListener(new TextWatcher() 
{           
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {               
    }           
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {            
    }           
    @Override
    public void afterTextChanged(Editable s) 
    {
        Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");

        Matcher matcher = mPattern.matcher(s.toString());               
        if(!matcher.find())
        {
            weightEditText.setText(); // Don't know what to place                   
        }
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:34:31+00:00Added an answer on June 2, 2026 at 5:34 pm

    There’s never any point in examining dest alone in an InputFilter; that’s what’s already present in the field. Change the regular expression match to be against source and it would be appropriate if you only wanted to check that certain characters were accepted into the field. However, you want to check field formatting, not just filter the input on a character-by-character basis. This is much more complex.

    Every time the user makes a change to the contents of tempEditText, the system calls your filter’s filter method before the change is actually made. It passes the current field contents and the proposed change (which can be insert/append, delete, or replace). The change is represented by a source CharSequence source (the characters—if any—to be added to the field), range start and end indexes within the source (the range is not necessarily all of source), a Spanned dest (the current field contents before the change) and range dstart and dend indexes within dest that are proposed to be replaced by the indicated source range.

    The job of filter is to modify the change (if necessary) and return a CharSequence to use (in its entirety) in place of source (or null to go ahead and use source). Rather than checking dest as you are now doing, you will need to check whether the change will result in an acceptable field. To do this, you will need more complex logic. (Note, in particular, that the new character(s) may be intended for insert somewhere other than at the end; also, filter will be called when the user is deleting characters as well as adding them.)

    It may be easier to implement a TextWatcher. In it’s beforeTextChanged method, you can record the current contents and in it’s afterTextChanged method, you can check (using a regular expression) whether the contents are acceptable and, if not, restore the before-the-change contents. (Make sure, though, that the text before the change was acceptable. If it isn’t, substitute something acceptable—like clearing the field. Otherwise your code will go into an infinite loop because the TextWatcher is going to be invoked again when you correct the field contents.)

    You also have an error in your regular expression: it allows a leading zero. Here’s an improved version that fixes this problem (and removes one set of unnecessary parentheses):

    "^([1-9][0-9]{0,2})?(\\.[0-9]?)?$"
    

    (As an aside: you can use \\d instead of [0-9].)

    EDIT

    Here’s my edit of your edit:

    weightEditText.addTextChangedListener(new TextWatcher() 
    {           
        private static final Pattern sPattern
            = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");
    
        private CharSequence mText;
    
        private boolean isValid(CharSequence s) {
            return sPattern.matcher(s).matches();
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){
        }           
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after){
            mText = isValid(s) ? new CharSequence(s) : "";
        }           
    
        @Override
        public void afterTextChanged(Editable s) 
        {
            if (!isValid(s))
            {
                weightEditText.setText(mText);
            }
            mText = null;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have Edittext which can take Decimal value. So I have used android:inputType=numberDecimal android:numeric=decimal
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have set input type to be numberdecimal but also want to populate the
Have just started using Google Chrome , and noticed in parts of our site,
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
Using my onEditTextchanger. It works fine when the user inputs 100000, in the EditText
I am working on a C# application. I want to change number decimal figure
Have finally got a responsive site working (of a fashion). What I want to
Have some code: using (var ctx = new testDataContext()) { var options = new
Have a group of related projects running in SQL Server 2005 for which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.