Jump to content

Little security check snippet


Parkho

Recommended Posts

  • Moderators

Hello guys,

This is a little security snippet you can use for your website. The user needs to answer the multiplication question before proceeding:

 

PHP:

$rand1 = rand(1, 9);
$rand2 = rand(1, 9);

HTML:

<div>
	<label><?php echo $rand1;?> x <?php echo $rand2;?> =   </label>
	<input type="text" class="form-control" name="sum" id="ques" placeholder="<-- Your Answer" required>
  	<input id="sendbtn" type="submit" name="submit" value='Send' disabled>
</div>

Jquery to check user's answer. If the answer is correct, as soon as the user move the mouse out of the field, the "Send" button is enabled otherwise it's disabled:

$("#ques").on('mouseout', function() {
  var sumques = $("#ques").val();
  var val1 = <?php echo $rand1;?>;
  var val2 = <?php echo $rand2;?>;
  var sumOfValues = val1 * val2;
  
  if (sumOfValues == sumques) 
	{
		$("#sendbtn").prop('disabled', false);
	}
  else
  	{
		$("#sendbtn").prop('disabled', true);
 	}  
});

Let me know what you think.

Cheers

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...