javascript - How to Pass the Email Id value after checking Captcha in Asp.Net Mvc4? -
i new 1 asp.net mvc4 entity framework. doing captcha verification forgot password. code, passing email id value controller when clicking submit button captcha code invalid. want pass email id value controller if captcha code correct otherwise should show validation error , new captcha should reloaded. please me fix it. in advance.
this java script code generation , validating captcha:
var captchastring = ''; function getcaptcha() { var chars = "0aa1bb2cc3dd4ee5ff6gg7hh8ii9jj0kk1ll2mm3nn4oo5pp6qq7rr8ss9tt0uu1vv2ww3xx4yy5zz"; var string_length = 7; captchastring = ''; (var = 0; < string_length; i++) { var rnum = math.floor(math.random() * chars.length); captchastring += chars.substring(rnum, rnum + 1); } document.getelementbyid("randomfield").innerhtml = captchastring; } function validation() { var text2 = document.getelementbyid("txtcode").value; if (text2 == captchastring) { var email = document.getelementbyid("useremail").value; x = document.getelementbyid("demo"); // find element x.innerhtml = "valid"; x.style.color = "#ff0000"; } else { x = document.getelementbyid("demo"); // find element x.innerhtml = "invalid captcha. try again"; x.style.color = "#ff0000"; } } </script>
this body of cshtml code:
<div class="col-md-5"> @using (html.beginform()) { @html.validationsummary(true) <h5 class="nomargin">forgot password</h5> @html.textboxfor(u => u.useremail, new { @class = "form-control", placeholder = "email" }) <br /> <div id="captcha"> <div id="captcha_gen"> <label id="randomfield"> </label> </div> <button type="button" onclick="getcaptcha();" style="border: 0; background: transparent;float:right; position:relative";> <img src="../../content/fslbootstrapui/images/playback_reload.png" width="25" height="25" alt="submit" /> </button> </div> <input type="text" id="txtcode" class="form-control" placeholder="enter code here" /> <button class="btn btn-success btn-block" value="submit" onclick="validation()">reset</button> <p id="demo"> </p> } </div>
what happening currently? mean, how application behaving? edit: can try server side validation example if have field validate can add validation tag there.for example:
<input type="text" name="sampletextbox" id="sampletextboxid"/> @html.validationmessage("sampletextbox", "*")
then go controller , add kind of code:
if (!string.isnullorempty(sampletextbox)) { //your code. } else { viewdata.modelstate.addmodelerror("sampletextboxid", "text should not empty."); }
use model.isvalid
condition write main code. model.isvalid
becomes false if viewdata.modelstate.addmodelerror("sampletextboxid", "text should not empty.");
executed. way add validations. can check valid/invalid captcha in controller , throw error. example:
if (isvalidcaptcha(enteredcaptcha)) { //code } else { viewdata.modelstate.addmodelerror("captchaid", "enter valid captcha"); }
lastly, add validation summary page
@html.validationsummary("error messages")
Comments
Post a Comment