Javascript Input Checking
I need you help with a piece of javascript that enables input and result checking from:
Input attributes will be similar to this on the HTML doc.
<div id="game">
<div id="input1"&g开发者_JS百科t;<input name="01_01" onchange="0" type="text"/></div>
I need a tough script to checke againts 81 numbers definied on another array and also need an handler to check the users input againts the answers stored on that array from input boxes above.
Any help is always appreciated.
Cheers
Here's one way:
var input = '800500614300000002169074050045020000903000040621008900008100000400060000000039800'.split('');
var answer = '872593614354681792169274358745926783983715246621348975238157469497862531516439827'.split('');
var result = (function puzzleStatus(){
if( input.join() === answer.join() ) return 'Solved!';
for( var i = 0, j = input.length ; i <= j; i++ ){
if( input[i] !== '0' && input[i] !== answer[i] ){
return 'There\'s a Mistake';
}
};
return 'Correct so far';
}()); // Yields "Correct so far" with above inputs
Edit: Updated the function to report whether the puzzle is solved, unsolved but correct so far, or contains an error.
Example: http://jsfiddle.net/redler/skxHm/1/
You can use jquery to retrieve all the inputs. Then read each one and stuff it in a string. Finally do a string compare against your answer key to determine whether it is correct.
http://jsfiddle.net/mrtsherman/RHzKT/
var answers = "";
$("input:text").each( function() {
answers += $(this).val();
});
精彩评论