Created
February 18, 2020 10:00
-
-
Save tayormi/aae7ad5fb412e8e841c92b2d250d989b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static String validateCardNum(String input) { | |
| if (input.isEmpty) { | |
| return Strings.fieldReq; | |
| } | |
| input = getCleanedNumber(input); | |
| if (input.length < 8) { | |
| return Strings.numberIsInvalid; | |
| } | |
| int sum = 0; | |
| int length = input.length; | |
| for (var i = 0; i < length; i++) { | |
| // get digits in reverse order | |
| int digit = int.parse(input[length - i - 1]); | |
| // every 2nd number multiply with 2 | |
| if (i % 2 == 1) { | |
| digit *= 2; | |
| } | |
| sum += digit > 9 ? (digit - 9) : digit; | |
| } | |
| if (sum % 10 == 0) { | |
| return null; | |
| } | |
| return Strings.numberIsInvalid; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment