Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validation date in EditText for android while user is clicking
//SOURCE: http://stackoverflow.com/questions/16889502/how-to-mask-an-edittext-to-show-the-dd-mm-yyyy-date-format
EditText date;
date = (EditText)findViewById(R.id.whichdate);
date.addTextChangedListener(tw);
TextWatcher tw = new TextWatcher() {
private String current = "";
private String ddmmyyyy = "DDMMYYYY";
private Calendar cal = Calendar.getInstance();
When user changes text of the EditText
@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;
date.setText(current);
date.setSelection(sel < current.length() ? sel : current.length());
}
}
We also implement the other two functions because we have to
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
};
@hemapanjakshram

This comment has been minimized.

Copy link

@hemapanjakshram hemapanjakshram commented Nov 27, 2019

when one character in middle is deleted cursor deletes the last character.how to fix this?

@premspal

This comment has been minimized.

Copy link

@premspal premspal commented Mar 17, 2021

when one character in middle is deleted cursor deletes the last character.how to fix this?

did you find solution ? if yes plz share..

@leosmaia21

This comment has been minimized.

Copy link

@leosmaia21 leosmaia21 commented May 23, 2021

hello can u add hour and minute? like dd/mm/yyyy hh:mm.
thank u

@lancerkind

This comment has been minimized.

Copy link

@lancerkind lancerkind commented Sep 20, 2021

TDD Academy is building a SSN TextWatcher which uses the same approach, but they are doing it by adding unit tests. @premspal @hemapanjakshram we'll defiantly fix that bug.
https://www.twitch.tv/tddacademy
https://m.youtube.com/channel/UCU1U6oPscxzKd_IWAIOm60w

The repo is at: https://github.com/lancerkind/Android-TDD-SSNTextBox/tree/usingOnTextChanged

@lancerkind

This comment has been minimized.

Copy link

@lancerkind lancerkind commented Sep 20, 2021

Regardless of the bug, the above code is pretty close. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment