英文:
Detect auto fill in a text input on react native
问题
在React Native中是否有可能检测到用户在文本输入框上使用了自动填充功能?
我有一个登录表单,如果他们从钥匙串等处自动填充了字段,我想提交登录。
我想要类似于onAutoFill这样的功能,但似乎并没有类似的东西。
<TextInput onChange={setEmail} autoComplete="email" />
<TextInput onChange={setPassword} autoComplete="password" secureTextEntry={true} />
我尝试过查看onChange事件,但似乎没有任何提示表明更改来自于自动填充。
英文:
Is it possible to detect when a user uses auto fill on a text input in react native.
I have a login form and I want to submit the login if they autofill the fields for example from the keychain.
I would love something like onAutoFill but there doesn't seem to be anything like that.
<TextInput onChange={setEmail} autoComplete="email" />
<TextInput onChange={setPassword} autoComplete="password" secureTextEntry={true} />
I've tried looking at the onChange event but there doesn't seem to be any hint that a change comes from autofill.
答案1
得分: 0
由于没有本机事件,最终采用了一种变通方法。
在这里,如果先前的值为空且新值至少为最小密码长度,我们将提交表单。
onChangeText={(newValue) => {
onChange(newValue);
// 注意:由于我们的密码必须至少为8个字符,如果字段变化了8个字符且之前为空,我们可以假设它是自动填充的
if (value.length === 0 && newValue.length >= 8) {
onSubmit();
}
}}
英文:
Ended up going with a bit of a workaround since there is no native event.
Here we submit the form if the previous value was empty and the new one is atleast the minimum password length.
onChangeText={(newValue) => {
onChange(newValue);
// NOTE since our passwords must be atleast 8 characters if the field changes by 8 characters and was empty we can assume it was autofilled
if (value.length === 0 && newValue.length >= 8) {
onSubmit();
}
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论