@Override public void afterTextChanged(Editable s) { // Note: we want to show an error state if the month is invalid or the // final, complete date is in the past. We don't want to show an error state for // incomplete entries. boolean shouldShowError = false; if (parts[0].length() == 2 && !DateUtils.isValidMonth(parts[0])) { // This covers the case where the user has entered a month of 15, for instance. shouldShowError = true; } // Note that we have to check the parts array because afterTextChanged has odd // behavior when it comes to pasting, where a paste of "1212" triggers this // function for the strings "12/12" (what it actually becomes) and "1212", // so we might not be properly catching an error state. if (parts[0].length() == 2 && parts[1].length() == 2) { boolean wasComplete = mIsDateValid; updateInputValues(parts); // Here, we have a complete date, so if we've made an invalid one, we want // to show an error. shouldShowError = !mIsDateValid; if (!wasComplete && mIsDateValid && mExpiryDateEditListener != null) { mExpiryDateEditListener.onExpiryDateComplete(); } } else { mIsDateValid = false; } setShouldShowError(shouldShowError); } });
/** * Clear all entered data and hide all error messages. */ public void clear() { mCardNumberEditText.setText(""); mExpiryDateEditText.setText(""); mCvcEditText.setText(""); mPostalCodeEditText.setText(""); mCardNumberEditText.setShouldShowError(false); mExpiryDateEditText.setShouldShowError(false); mCvcEditText.setShouldShowError(false); mPostalCodeEditText.setShouldShowError(false); updateBrand(Card.UNKNOWN); }
/** * Validates all fields and shows error messages if appropriate. * * @return {@code true} if all shown fields are valid, {@code false} otherwise */ public boolean validateAllFields() { boolean cardNumberIsValid = CardUtils.isValidCardNumber(mCardNumberEditText.getCardNumber()); boolean expiryIsValid = mExpiryDateEditText.getValidDateFields() != null && mExpiryDateEditText.isDateValid(); boolean cvcIsValid = isCvcLengthValid(); mCardNumberEditText.setShouldShowError(!cardNumberIsValid); mExpiryDateEditText.setShouldShowError(!expiryIsValid); mCvcEditText.setShouldShowError(!cvcIsValid); boolean postalCodeIsValidOrGone; if (mShouldShowPostalCode) { postalCodeIsValidOrGone = isPostalCodeMaximalLength(true, mPostalCodeEditText.getText().toString()); mPostalCodeEditText.setShouldShowError(!postalCodeIsValidOrGone); } else { postalCodeIsValidOrGone = true; } return cardNumberIsValid && expiryIsValid && cvcIsValid && postalCodeIsValidOrGone; }