So Safari offers Scan Credit Card feature on iOS8 with some credit card forms.
My question is, how does Safari determine when to offer this option?
So far I found that this option is available on Amazon and PayPal, but none of my credit card input forms were able to reproduce this behaviour.
I've found that something like this works, but I consider this a very ugly solution, since it depends on the actual displayed text between the
label
tags:I am not entirely sure, but I think, that
name
parameters are not important.This is now all broken after upgrading to iOS 8.1.3 this morning. When on iOS 8.1.2 all of the above worked just fine - now the keyboard option to scan credit card simply does not appear. Here's my code, which did work yesterday on iOS 8.1.2 and does not work today on iOS 8.1.3:
In addition to Arnaud Brousseau's answer, a search for "card number" in the iOS simulator files yields this file:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/SafariShared.framework/SafariShared
A quick run of
strings
on it reveals these strings which are certainly used for matching potential fields:and a bit further:
Can't quite see (with this naïve approach) any references to which attributes (
id
,name
,placeholder
...) or other metadata (label maybe?) are actually compared against this list. Also, with the exception of "name des karteninhabers", this is really very english-oriented, that's quite unusual for Apple IMHO.After a bit of research with an iOS8 browser and Chrome emulation I figured it out partially. I know of some solutions, but I don't know for sure if there are other ways to do it. You'll have to thank Apple for the amazing lack of documentation around this.
Currently Netflix/Amazon have credit card scanning working properly. So I emulated an iOS8 user agent in my Chrome browser and inspected the markup of their credit card number field. Here's Netflix's:
And here's Amazon's:
At that point I played around with a form served over HTTPS that I had control over and started setting attributes to see what would happen. Below, "works" means "successfully triggered card scan" and "doesn't work" means "did not trigger card scan":
name="addCreditCardNumber"
=> worksname="cardNumber"
=> worksname="cardnumber"
=> worksclass="cardNumber"
=> does not worktype="cardNumber"
=> does not workid="cardNumber"
,id="creditCardNumber"
,id="creditCardMonth"
,id="creditCardYear"
=> worksSince the
name
attribute drives form data and might impact the way server-side form processing works I highly recommend triggering credit card scan by setting your inputid
tocardNumber
.I would link to the relevant documentation...but hey! There's none (at least, not that I know of)
For the expiration fields, based on Arnaud's answer, I found that the expiration fields would be recognized from
cardExpirationYear
andcardExpirationMonth
being in theid
attribute.This worked when the year and month are regular text inputs with the appropriate IDs. The month is populated as a 2-digit number and the year as a 4-digit number.
In a quick test using <select> tags, I found that it also populated the correct month and year.
I don't know what other values will work in the option tags.
It's not about when, it's about how we can enable this feature in Safari browser.
Let's just talk about what happens when a form is submitted:
Some browsers stores all
input
values with it'sname
attribute. And it will offer to autocomplete those fields when it encounters samename
dinput
elements.Some browsers scan for just
autocomplete
attribute for offering auto-completion and,Some others scan for an attribute like
label
orname
forinput
elements too.Now,
autocomplete
attribute can have a larger set of values includingcc-name
(Card name),cc-number
,cc-exp
,cc-csc
(Security number - CVV) etc. (full list here)For example, we could say to a browser that, this is card number field and it should offer
autocomplete
when possible and it should enable scan credit card feature as:In general:
more detailed ex:
And each autocomplete values goes like this:
When we code it like this browser know exactly what kind of value should be populated in that field.
But browser like safari need
name
orid
orlabel
values should also be set right.Support so far for
autocomplete
,id
andname
attributes for auto-completing values.There are more things at play here. I strongly recommend this blog I referred.