0

i am able to set the date in DD/MM/YYYY format but i don't know how to validate this. When i click on edit text i will get DD/MM/YYYY and when i enter numbers those DD/MM/YYYY will get replaced but say i entered 11/MM/YYYY and then hit the submit it will get submitted. What can i do to prevent this until user enter complete date like 11/01/1990.Please help me out

This is my code :

 TextWatcher textwatcher = new TextWatcher() {

            private String current = "";
            private String ddmmyyyy = "DDMMYYYY";
            private Calendar cal = Calendar.getInstance();
            @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 (!s.toString().equals(current)) {
                    String clean = s.toString().replaceAll("[^\\d.]", "");
                    String cleanC = current.replaceAll("[^\\d.]", "");

                    int cl = clean.length();
                    int sel = cl;
                    for (int i = 2; i <= cl && i < 6; i += 2) {
                        sel++;
                    }
                    //Fix for pressing delete next to a forward slash
                    if (clean.equals(cleanC)) sel--;

                    if (clean.length() < 8){
                        clean = clean + ddmmyyyy.substring(clean.length());
                    }else{
                        //This part makes sure that when we finish entering numbers
                        //the date is correct, fixing it otherwise
                        int day  = Integer.parseInt(clean.substring(0,2));
                        int mon  = Integer.parseInt(clean.substring(2,4));
                        int year = Integer.parseInt(clean.substring(4,8));

                        if(mon > 12) mon = 12;
                        cal.set(Calendar.MONTH, mon-1);
                        year = (year<1900)?1900:(year>2100)?2100:year;
                        cal.set(Calendar.YEAR, year);
                        // ^ first set year for the line below to work correctly
                        //with leap years - otherwise, date e.g. 29/02/2012
                        //would be automatically corrected to 28/02/2012

                        day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day;
                        clean = String.format("%02d%02d%02d",day, mon, year);
                    }

                    clean = String.format("%s/%s/%s", clean.substring(0, 2),
                            clean.substring(2, 4),
                            clean.substring(4, 8));

                    sel = sel < 0 ? 0 : sel;
                    current = clean;
                    dob.setText(current);
                    dob.setSelection(sel < current.length() ? sel : current.length());
                }

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        };

        dob.addTextChangedListener(textwatcher);
Srikanth G
  • 461
  • 1
  • 4
  • 11
  • 3
    Use date picker control instead of edit text to enter date. Android have built in or you can find lots of libraries for more customization. – Wasim K. Memon Apr 14 '17 at 09:32
  • Hope this will help you -- http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression/ – Sachin Apr 14 '17 at 09:34
  • @androidnoobdev I would add that even if the OP resolves these issues, entering the date by hand is slow, error prone, and probably not user friendly. – Tim Biegeleisen Apr 14 '17 at 09:37
  • Possible duplicate of [Date Validation in Android](http://stackoverflow.com/questions/17416595/date-validation-in-android) – jediz Apr 14 '17 at 09:43
  • check my answer their https://stackoverflow.com/a/45810980/578309 – thanhbinh84 Aug 22 '17 at 07:04

0 Answers0