Allows you to automatically change input once it is filled in. Then validate the form when all inputs are filled in correctly.
With npm:
npm i auto-input-a2f
With pnpm:
npm i auto-input-a2f
With yarn:
yarn add auto-input-a2f
If you are using a web server such asexpressjs
,you need to set the file as static files to serve them. For example, forexpressjs
you'll need:
app.use(express.static('/node_modules/auto-input-a2f/dist/'));
In your html file:
<scriptsrc= "/autoinput.min.js"integrity= "sha384-p91U1bCVTJMP+YhKZTn2fYvOScTU3xokkfMUfc7A7XY5gndM1h2+8qbdtXICnS/b"crossorigin= "anonymous"></script>
For all other files, you canget the shashum below
<scriptsrc= "https://pioupia.github.io/auto-input-a2f/autoinput.min.js"integrity= "sha384-p91U1bCVTJMP+YhKZTn2fYvOScTU3xokkfMUfc7A7XY5gndM1h2+8qbdtXICnS/b"crossorigin= "anonymous"></script>
<scriptsrc= "https://pioupia.github.io/auto-input-a2f/autoinput.js"integrity= "sha384-fD5DnbGvuM3Yat97IQjMLbrbs0KGFPOAH9FMqB5ygLH6SALJvfT4s/UnDw/svN76"crossorigin= "anonymous"></script>
Formodule
type, you'll need to import correctly theAutoInput
class:
importAutoInputfrom"./autoinput.esm.js";
First of all, you need to instantiate theAutoInput
class:
constautoinput=newAutoInput(options);
The class could take someoptions
as anobject
.All options are optional.
name | type | default value | description |
---|---|---|---|
autoEnd |
boolean |
true |
Fired an event when the text is completed. |
selectAuto |
boolean |
true |
Will automatically focus the first input field (just after initializing fields). |
canPast |
boolean |
true |
Enable / disable pasting in fields. |
createAuto |
boolean |
false |
Will automatically create input field for you. |
buttonCallback |
boolean |
false |
Add theonClick listener on thevalidate button. If the user click on that one, this will trigger automatically thecallback function. |
beforeFire |
number |
400 |
Will waitsbeforeFire milliseconds before firing the automatic event. |
onCreate |
Function |
null |
The callback when creates the input fields. This allows you to add some property on theHTMLElement or change it completly. |
parent |
HTMLElement |
#a2fParent or[data-parent-a2f] |
The parent element into which the input fields will be injected. |
validate |
HTMLElement |
[data-button-validate] |
The submit button to validate the 2fa code. |
If you don't want thecreateAuto
option, you can create input yourself. But keep in mind that they need to have thedata-a2f
attributes.
This option will fired thecallback
function, and click on thevalidate
button when the 2FA code is completely filled.
Thecallback
functioncan be registered by calling the publiconValidate
method.
This option will automatically create the input fields for you. The elements will beappendChild
to theparent
HTMLElement. During the creation of the input fields, if theonCreate
callback is defined, it'll be fired for each input field.
The number of created field is 6.
If enabled, when theautoEnd
option is enabled, it will emit theonClick
event on thevalidate
HTMLButton.
The time in miliseconds waited by the class before calling thecallback
function,and fired theonClick
event.
This function will be called when the input fields are created. The function will take the createHTMLElement
as argument, and should also returned anHTMLElement
which will be added to the DOM. If nothing is returned from the function, the element will not be added.
This can be usefull if you want to add custom classes or attributes to the input fields. Or if you want to change the type of the input fields, by adding a more complex structure to it.
However, you can also completely remove the field if you do not want it.
This method will register thecallback
function. Thecallback
function will be called when the 2FA code is completely filled with theautoEnd
optionenabled, or if the user click on thevalidate
button and thebuttonCallback
optionis enabled.
Callback function prototype:
functiononValidate(code:string):void;
This function allows you to get the 2FA code. It will return astring
with the 2FA code.
If all fields are not complete, the function will still return a string with the fields filled in.
For example, if your field 0, 1, 3 and 5 are field with:
5 2. 4. 1
,the function will return5241
.
Function prototype:
functiongetCode():string;
Allows you to clear each input fields values. Just a simple for reset.
Function prototype:
functionremoveEntries():void;
This function will disable theautoEnd
option. This can be usefull if you want to disable the automatic validation when the 2FA code is completely filled.
Function prototype:
functionstopAutoEnd():void;
This function will enable theautoEnd
option. This can be usefull if you want to enable the automatic validation when the 2FA code is completely filled and you got an error for example.
Function prototype:
functionstartAutoEnd():void;
This function will toggle theautoEnd
option.
Function prototype:
functiontoggleAutoEnd():void;
For example, you want to create automatically 6 input fields, and validate the form when all fields are filled in correctly after 800 miliseconds.
Because we have thebuttonCallback
enabled, if the user click on the validation button, theonValidate
function will be automatically called.
But we can have a problem if the user is going to click on thevalidate
button before the 800 miliseconds. To avoid this, we can disable thebuttonCallback
option by calling thestopAutoEnd
method during the server verification.
⚠️ This will not disabled theonValidate
function call when the user click on it.
For each created input fields, we want to add some classes to it. We can do this by using theonCreate
callback.
If the input field is aSPAN
,we don't want aspan
element (the dash-
between the input fields) to be added to the DOM. So we can returnnull
in this case.
Otherwise, we'll add some classes to our element. But if the inserted element is the fourth one, we'll add a margin to the left. Then, we'll return the element.
constinput=newAutoInput({
createAuto:true,
parent:document.getElementById("2fa_container"),
validate:document.getElementById("validate_2fa_code"),
buttonCallback:true,
beforeFire:800,
onCreate:(element,i)=>{
if(element.tagName==="SPAN")
return(null);
element.classList.add("form-control");
element.classList.add("otp_input");
element.classList.add(i===3?"ms-3":"ms-1");
element.classList.add("me-1");
element.classList.add("text-center");
return(element);
}
});
functiononValidate(code){
// do something with the code...
input.stopAutoEnd();
fetch("/api/code",{
method:"POST",
body:JSON.stringify({code}),
headers:{
"Content-Type":"application/json"
}
}).then(res=>{
if(res.ok){
// do something...
}else{
// do something...
// enable the autoEnd
input.startAutoEnd();
// clear the input fields
input.removeEntries();
}
});
}
input.onValidate(onValidate);
For example, you want a static complex structure, or have a different of input fields, that'll not be generated by JavaScript or by the script because it's too complex.
You can create your complex structure. Just think of the only thing needed byauto-input-a2f
is that yourinput
field exists and has at least adata-a2f
attribute.
<divid= "2fa_container">
<div>
<inputtype= "text"data-a2f>
</div>
<div>
<inputtype= "text"data-a2f>
</div>
<div>
<inputtype= "text"data-a2f>
</div>
<div>
<inputtype= "text"data-a2f>
</div>
<buttonid= "validate_2fa_code">Valdidate</button>
</div>
Same thing than for the previous example:
constinput=newAutoInput({
parent:document.getElementById("2fa_container"),
validate:document.getElementById("validate_2fa_code"),
buttonCallback:true,
beforeFire:800
});
functiononValidate(code){
// do something with the code...
input.stopAutoEnd();
fetch("/api/code",{
method:"POST",
body:JSON.stringify({code}),
headers:{
"Content-Type":"application/json"
}
}).then(res=>{
if(res.ok){
// do something...
}else{
// do something...
// enable the autoEnd
input.startAutoEnd();
// clear the input fields
input.removeEntries();
}
});
}
input.onValidate(onValidate);
And you'll have the same behavior. Just, the only difference is that you'll only have 4 fields.