3 ms·
It's common for one very simple reason: Programatically, it is much easier to demand user input in a single form than to code for different cases. In this speci
by tdavis 18y ago
It's common for one very simple reason: Programatically, it is much easier to demand user input in a single form than to code for different cases. In this specific case I don't understand it so much because it's exceedingly simple to write a regex replace that replaces every instance of "not a number" with nothing. The linked page describes a less-general use of this in Perl, but it's just as easy in any other modern language.
Here's a single line of python that removes any character that isn't a digit from the string "s":
re.sub(r'[^\d]', '', s)
Wow, that was almost too easy. Now the user can put spaces, dashes, periods, underscores... who cares! Oh, and if they put in the CSV code or something at the end...
re.sub(r'[^\d]', '', s)[0:16]
A single line should cover 99% of the problems the user can create. Compliment it by giving an example of how the number should be input and... done.
- catbells 18y agoSome cards have 19 digit numbers. (If all the cards you accept always have 16 digit numbers then you don’t have to worry about that, of course.)