Thursday, June 4, 2015

How to set simple form captcha through js

<script>
function myFunction() {
    var x = Math.floor((Math.random() * 9) + 1);

    document.getElementById("cap1").innerHTML = x;
 var y = Math.floor((Math.random() * 9) + 1);
document.getElementById("cap2").innerHTML = y;
var z= x+y;
document.getElementById("hid_cap1").value = z;

}
function valcpa() {
var hidc = document.getElementById("hid_cap1").value;
var orgc = document.getElementById("org_cap1").value;
if(orgc=="")
{
document.getElementById("cap_msg").innerHTML = "*Please Enter captcha value.";
return false;
}
else if(hidc!=orgc)
{
document.getElementById("cap_msg").innerHTML = "*Please Enter sum of both value.";
return false;
}

else {
document.getElementById("cap_msg").innerHTML = "";
 }
}
</script>



<body onload="myFunction();">
<form action="" method="post">
 <table>
 <tr>
  <td><label><span id="cap1"></span>+<span id="cap2"></span> =</label></td>
  <td><input type="hidden" id="hid_cap1" name="hid_cap" value="" /><input type="text" name="org_cap" id="org_cap1" required="required" />
  <p id="cap_msg" style="color:#f00;"></p>
  </td>
  </tr>
 
  <tr>
  <td colspan="2">
 <input type="SUBMIT" name="" value="Submit"    CLASS="Submit-d-ship " onclick="return valcpa();">
  </td>
  </tr>
  </table>
  </form>
</body>

No comments :

Post a Comment