I have two edittexts. I want to validate date entered in first edittext when I switch to next edittext... Is it possible? I want to validate in dd/mm/yyyy format strictly.. please help me.. i searched and tried also but not get such a solution..
Asked
Active
Viewed 1.1k times
5
-
what effort you put for this? tell more about your date format – Sree Mar 26 '14 at 11:43
-
2Better to use [DatepickerDialog](http://stackoverflow.com/questions/18267091/open-a-datepickerdialog-on-click-of-edittext-and-set-the-date-value-android) instead of Edittext for date inputs..' – Chirag Ghori Mar 26 '14 at 11:43
-
only after api 21 it is ok to call the Calendar class... – obeliksz Jun 22 '17 at 12:42
4 Answers
1
Step 1
youredittextname.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Is_Valid_date(name); // pass your EditText Obj here.
}
});
Step 2
Write a function.
public void Is_Valid_date(EditText edt) throws NumberFormatException {
if (edt.getText().toString().length() <= 0) {
edt.setError("Accept number Only.");
valid_name = null;
} else if (check your date format using simpledate format) {
edt.setError("Accept Alphabets Only.");
valid_name = null;
} else {
valid_name = edt.getText().toString();
}
}
If you need to know Validate of the date. Please visit this link.
http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression/
Thank you.

Andrea Lazzarotto
- 2,471
- 1
- 22
- 38

Sethu
- 430
- 2
- 13
-
thank you sethu.. Can You tell me how to check date format using simpledate format.. please sir – user3459783 Mar 26 '14 at 12:13
-
refer below link boss you will get it. study simple date format.http://stackoverflow.com/questions/17416595/date-validation-in-android – Sethu Mar 26 '14 at 12:28
-
sir I resolved it... Thank You so much.. your post helped me alot.. thank you once again... – user3459783 Mar 26 '14 at 12:40
-
boss no sir and all okay. you are the most welcome person. all the best do well. – Sethu Mar 26 '14 at 12:59
1
private static final String DATE_PATTERN="(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
private Pattern pattern= Pattern.compile(DATE_PATTERN);
public boolean isValidDate(final String date) {
Matcher matcher = pattern.matcher(date);
if (matcher.matches()) {
matcher.reset();
if (matcher.find()) {
String day = matcher.group(1);
String month = matcher.group(2);
int year = Integer.parseInt(matcher.group(3));
if (date.equals("31") && (month.equals("4")
|| month.equals("6")
|| month.equals("9")
|| month.equals("11")
|| month.equals("04")
|| month.equals("06")
|| month.equals("09"))) {
return false;
} else if (month.equals("2") || month.equals("02")) {
if (year % 4 == 0) {
if (day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
} else {
if (day.equals("29") || day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
}
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}

ilidiocn
- 323
- 2
- 5
0
Use interface TextWatcher
in Android. You have to override its methods:
- In
onTextChanged()
limit the length of text - In
afterTextChanged()
use some good regular expression for date validation. It will be available from Google.

Andrea Lazzarotto
- 2,471
- 1
- 22
- 38

Don Chakkappan
- 7,397
- 5
- 44
- 59