A robust Android library for validating payment card information in real-time with support for multiple card types and comprehensive error handling.
- ✅ Real-time card number validation
- ✅ Card type detection (Visa, Mastercard, American Express, etc.)
- ✅ Expiry date validation
- ✅ CVV/CVC validation
- ✅ Cardholder name validation
- ✅ Lightweight and efficient
- ✅ Easy-to-integrate API
Add the dependency to your module's build.gradle:
dependencies {
implementation 'com.tap:cardvalidator:1.0.0'
}<dependency>
<groupId>com.tap</groupId>
<artifactId>cardvalidator</artifactId>
<version>1.0.0</version>
</dependency>import com.tap.cardvalidator.CardValidator
val validator = CardValidator()val cardNumber = "4532015112830366"
val isValid = validator.validateCardNumber(cardNumber)
if (isValid) {
val cardType = validator.detectCardType(cardNumber)
println("Card Type: $cardType")
} else {
println("Invalid card number")
}val month = 12
val year = 25
val isExpiryValid = validator.validateExpiryDate(month, year)
if (isExpiryValid) {
println("Expiry date is valid")
} else {
println("Card has expired or invalid format")
}val cvv = "123"
val cardType = "VISA"
val isCVVValid = validator.validateCVV(cvv, cardType)
if (isCVVValid) {
println("CVV is valid")
} else {
println("Invalid CVV")
}val cardholderName = "John Doe"
val isNameValid = validator.validateCardholderName(cardholderName)
if (isNameValid) {
println("Cardholder name is valid")
} else {
println("Invalid cardholder name")
}import com.tap.cardvalidator.CardValidator
class PaymentActivity : AppCompatActivity() {
private val validator = CardValidator()
fun processPayment(
cardNumber: String,
expiryMonth: Int,
expiryYear: Int,
cvv: String,
cardholderName: String
): Boolean {
// Validate all fields
if (!validator.validateCardNumber(cardNumber)) {
showError("Invalid card number")
return false
}
if (!validator.validateExpiryDate(expiryMonth, expiryYear)) {
showError("Invalid expiry date")
return false
}
val cardType = validator.detectCardType(cardNumber)
if (!validator.validateCVV(cvv, cardType)) {
showError("Invalid CVV")
return false
}
if (!validator.validateCardholderName(cardholderName)) {
showError("Invalid cardholder name")
return false
}
// All validations passed
submitPayment(cardNumber, expiryMonth, expiryYear, cvv, cardholderName)
return true
}
private fun showError(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
private fun submitPayment(vararg args: Any) {
// Process payment
}
}- Visa
- Mastercard
- American Express
- Discover
- Diners Club
- JCB
- Maestro
| Method | Parameters | Returns | Description |
|---|---|---|---|
validateCardNumber() |
cardNumber: String | Boolean | Validates card number using Luhn algorithm |
detectCardType() |
cardNumber: String | String | Detects and returns card type |
validateExpiryDate() |
month: Int, year: Int | Boolean | Validates expiry date |
validateCVV() |
cvv: String, cardType: String | Boolean | Validates CVV based on card type |
validateCardholderName() |
name: String | Boolean | Validates cardholder name |
- Always validate on the client side - Provides immediate user feedback
- Never store card details - Use tokenization for production
- Validate expiry before processing - Check for expired cards
- Use appropriate card type validation - CVV length differs by card type
- Handle validation errors gracefully - Show clear error messages to users
- Test with multiple card numbers - Use test cards for each type
Use these test card numbers for development:
Visa: 45xxxxxxxx2830366
Mastercard: 54xxxxxxxxxxxx0000
American Express: 37xxxxxxxxxxxxxx126
Discover: 6xxxxxxxxxxxx7
- Solution: Ensure card number is stripped of spaces and hyphens
- Solution: Verify the card number length matches the card type
- Solution: AmEx CVV is 4 digits, not 3
- Solution: Use the correct card type when validating
- Solution: Ensure month is 1-12 and year is valid
- Solution: Use 2-digit year format (e.g., 25 for 2025)
This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or contributions, please visit our GitHub repository.