Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate phone number and amount

I have been trying to figure the appropriate way to represent phone number and amount. Let me be specific, I have a user form where the user are supposed to fill their phone number and amount. I am from Nepal and we usually have the phone number in format 01-4444445 or 023-454566. I wonder how can I do this? In addition, in the amount field, i wish to display amount in this format 1,000,000 and so on. For example, when the user enters four digit comma sign should be automatically added to the field. Plz, suggest me the way I can do this. I have been trying this since a week. Thanks in advance.

like image 667
therameshbista Avatar asked Feb 03 '26 11:02

therameshbista


1 Answers

Call this function from TextWatcher (For NTC Prepaid validation) "^(984|986)\d{7}$" this means phone number starts with 984 or 986 and after that there is 7 digits i.e. total 10 digits

phoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!isValidNumber(s.toString())){
                    isnumbervalid=false;
                    phoneNumber.setTextColor(Color.parseColor("#F44336"));
                }else {
                    isnumbervalid=true;
                    phoneNumber.setTextColor(Color.parseColor("#000000"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    private boolean isValidNumber(String phonenumber) {
            String PHONE_PATTERN = "^(984|986)\\d{7}$";

            Pattern pattern = Pattern.compile(PHONE_PATTERN);
            Matcher matcher = pattern.matcher(phonenumber);
            return matcher.matches();
        }
like image 66
taman neupane Avatar answered Feb 06 '26 01:02

taman neupane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!