Reputation: 41
So, I wanted to create captcha that generates 5 random numbers inside the text input on every page reload. When I run the function, it wont work, so I'm wondering why.
Here is the HTML:
<input type="text" id="ver" onload="captchaCreate()" disabled></input>
Here is the function in javascript:
function captchaCreate(){
var niz = new Array(5);
for (var i; i<=5; i++){
niz[i] = Math.floor(Math.random()*10);
}
document.getElementById("ver").value = niz.join("");
}
Upvotes: 0
Views: 652
Reputation: 15662
As mentioned in the comments, creating a clientside captcha is pretty useless
As for your code, inputs don't have an onload event and you need to assign the var you want to loop because undefined + 1
is NaN
window.onload = function() {
captchaCreate();
}
function captchaCreate(){
var niz = new Array(5);
for (var i=0; i<5; i++){
niz[i] = Math.floor(Math.random()*10);
}
document.getElementById("ver").value = niz.join("");
}
<input type="text" id="ver" disabled></input>
Upvotes: 1
Reputation: 500
to load the fonction when you refresh the page you can use
window.onload = function() {
captchaCreate();
}
or just :
captchaCreate();
you can do this with JS or PHP, but PHP launches once when loading the page (you said that you need to call the function only when you refresh the page. So if you want you can use php ^^ )
this works good for me :
function captchaCreate(){
var ar = new Array(5);
for (var i=0; i<5; i++){
ar[i] = Math.floor(Math.random()*10);
}
document.getElementById("your_div").value = ar.join("");
}
Upvotes: 0