Author: | Alex Drummond |
---|---|
Mode: | javascript |
Date: | Mon, 29 Nov 2010 22:04:10 |
// // Example items: // ["blank", "Form", blank("This is a sentence with a ________ blank _______ thing.")], ["blank", "Form", blank("This is another sentence ________ blanked out.")] // // Add this code at the top of the data file: // function blank(a, b) { var sentence = b ? b : a; var n = b ? a : null; var seq = [""]; var inBlank = false; for (var i = 0; i < sentence.length; ++i) { var c = sentence.charAt(i) if (inBlank) { if (c == '_') (seq[seq.length-1])++; else { seq.push(c); inBlank = true; } } else { if (c != '_') seq[seq.length-1] += c else { seq.push(1); inBlank = true; } } } var ihtml = ""; var bcount = 0; for (var i = 0; i < seq.length; ++i) { if (typeof(seq[i]) == "number") { ++bcount; var input = " <input type='text' name='blank-" + bcount + "' size='" + (n ? n : seq[i]) + "'></input> "; ihtml += input; } else { ihtml += $("<div>").text(seq[i])[0].innerHTML; } } var e = "<p>"; var validators = { }; var bcount = 0; for (var i = 0; i < seq.length; ++i) { if (typeof(seq[i]) == "number") { ++bcount; e += "<label class='error' for='blank-" + bcount + "'></label>"; validators['blank-' + bcount] = function (s) { if (s && ! s.match(/^\s*$/)) return true; else return "You must fill in the blank."; } } } e += "</p>" return { html: "<p>" + ihtml + "</p>" + e, validators: validators }; }