/*--------------------------------------------------------
- Simple Captcha/Random Code Generator
- by Uunknownn
- See working at https://jsfiddle.net/uo5s12hf/
--------------------------------------------------------*/
var generateCaptcha = (function(){
// Allowed alpha/numeric characters
var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var n = "0123456789";
// Captcha length
var cap_size = 5;
// Generate a random number from 0 to X
var rand = function(x){
return Math.floor(Math.random() * x);
}
// Build the captcha
var captcha = "";
while (cap_size--) {
// if 0 add a random alphabetic character
if (rand(2) === 0) captcha += a[rand(a.length)];
// if 1 add a random numeric character
else captcha += n[rand(n.length)];
}
// Return the captcha when it's done
return captcha;
})();
// Print on screen
document.body.innerHTML = generateCaptcha;
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.