1

I have a question.... I'm wondering is it possible to make an edit text field in android which is during the input, if it's not dateformat dd/mm/yyyy inserted it will be rejected somehow.... How can i compare the input text with the format that i wanted ?? Any suggestion ???? I dont think i need to post my code, since what i want is just a general thing, i just need an example or something like that, but i dont know how to do it. Many example use the date picker, but i dont want to use that... I want to input it manually... Give me some enlightment please...

Oh yeah, one more thing, i cant find edit text field with currency format. Is it not existed ?

Charles Lynch
  • 221
  • 1
  • 2
  • 8

3 Answers3

1

Uses a TextWatcher to listen for changes to the inputted string, and then format the string with DateFormat and see if it fits the desired format.

editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // you can check for validity here
});
Kai
  • 15,284
  • 6
  • 51
  • 82
1

Give it a try.

    public void checkFormate(final EditText mEditText) {

           mEditText.addTextChangedListener(new TextWatcher() {
          @Override
           public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            SimpleDateFormat mdaDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd");
            try {
                mdaDateFormat.parse((String) arg0);
            } catch (ParseException e) {
                e.printStackTrace();
                mEditText.setError("Please enter proper date format");
            }
            }

           @Override
           public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

            }

          @Override
           public void afterTextChanged(Editable arg0) {

           }
       });
  }
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • @CharlesLynch Do not forget to mark answer if its correct and up vote for people if they posted some helpful answer to appreciate their help. – Tofeeq Ahmad Jul 30 '13 at 09:48
1
public boolean isValidDate(String date)

{
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");   
    Date testDate = null;      
    try  
    {
      testDate = sdf.parse(date);
    }
    catch (ParseException e)   
    {
      errorMessage = "the date you provided is in an invalid date" +
                              " format.";
      return false;
    } 
    if (!sdf.format(testDate).equals(date))
    {
      errorMessage = "The date that you provided is invalid.";
      return false;
    }
    return true;   
} 
selva_pollachi
  • 4,147
  • 4
  • 29
  • 42