本文介绍了如何在Asp.Net Mvc4检查验证码后,通过电子邮件ID值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新一个Asp.Net Mvc4与实体框架。我现在做验证码验证忘记密码。正如我的code,我是值传递的电子邮件ID,当我点击提交按钮连验证码code是无效的控制器。我想通过价值的电子邮件ID到控制器,如果验证码code是正确的,否则它应该显示验证错误和新的captcha应该得到重新加载。请帮我解决它。先谢谢了。

这是我的Java脚本code生成和验证验证码:

  VAR captchastring ='';
        功能getCaptcha(){
            VAR字符=0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj0Kk1Ll2Mm3Nn4Oo5Pp6Qq7Rr8Ss9Tt0Uu1Vv2Ww3Xx4Yy5Zz;
            VAR字符串长度= 7;
            captchastring ='';
            对于(VAR I = 0; I<字符串长度,我++){
                VAR RNUM = Math.floor(的Math.random()* chars.length);
                captchastring + = chars.substring(RNUM,RNUM + 1);
            }
            的document.getElementById(randomfield)的innerHTML = captchastring。
        }        功能验证(){
            VAR文本2 =的document.getElementById(TXT code)的价值。
            如果(文本2 == captchastring){                VAR电子邮件=的document.getElementById(USEREMAIL)值。                X =的document.getElementById(演示);
                //查找元素                x.innerHTML =有效;
                x.style.color =#FF0000;            }
            其他{                X =的document.getElementById(演示); //查找元素
                x.innerHTML =无效的验证码重试;
                x.style.color =#FF0000;
            }
        }     < / SCRIPT>

这是我的我的CSHTML code的身体:

 < D​​IV CLASS =COL-MD-5>
                    @using(Html.BeginForm())
                    {
                                    @ Html.ValidationSummary(真)
                    < H5类=nomargin>忘记密码< / H5>                         @ Html.TextBoxFor(U => u.UserEmail,新{@class =表格控,占位符=电子邮件})
                        < BR />
                        < D​​IV ID =验证码>
                            < D​​IV ID =captcha_gen>
                                <标签ID =randomfield>
                                < /标签>
                            < / DIV>
                            <按钮式=按钮的onclick =getCaptcha();风格=边界:0;背景:透明;浮动:权利;位置:亲属;>
                                < IMG SRC =../../内容/ FSLBootstrapUI /图像/ playback_reload.pngWIDTH =25HEIGHT =25ALT =提交/>
                                     < /按钮>
                        < / DIV>                        <输入类型=文本ID =TXT code级=表格控占位=请在此输入code/>                        <按钮类=BTN BTN-成功BTN-块值=提交的onclick =验证()>复位< /按钮> < p n =演示> &所述; / P>
                    }
                < / DIV>


解决方案

什么是目前发生的事情?我的意思是,如何在应用程序的行为?
编辑:
您可以尝试例如服务器端验证,如果你有一个字段来验证您可以添加一个验证标签there.For例如:

 <输入类型=文本名称=SampleTextBoxID =SampleTextBoxId/>
 @ Html.ValidationMessage(SampleTextBox,*)

那你去给控制器,并添加这种code的:

 如果(!string.IsNullOrEmpty(SampleTextBox))
{
//你的code。
}
  其他
{
  ViewData.ModelState.AddModelError(SampleTextBoxId,文本不应该是空的。);
}

使用 Model.IsValid 作为条件来写你的主要code。
  Model.IsValid 变成假,如果 ViewData.ModelState.AddModelError(SampleTextBoxId,文本不应该是空的。); 被执行。
这是添加验证的方法。你可以在你的控制器检查您的有效/无效的验证码本身并抛出错误。
例如:

 如果(IsValidCaptcha(enteredCaptcha))
{
// code
}
其他
{
ViewData.ModelState.AddModelError(captchaId,输入有效的验证码);
}

最后,添加一个验证摘要到您的网页

  @ Html.ValidationSummary(错误消息)

I am new one to Asp.Net Mvc4 with Entity Framework. Now i am doing Captcha verification for Forgot Password. As my code, I it is passing the Email id value to Controller when i am clicking submit button even the Captcha code is Invalid. I want to pass the Email id value to controller if the Captcha code is correct otherwise it should show the validation error and New captcha should get reloaded. Please help me to fix it. Thanks in advance.

This is my Java Script code for generation and validating the captcha:

var captchastring = '';
        function getCaptcha() {
            var chars = "0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj0Kk1Ll2Mm3Nn4Oo5Pp6Qq7Rr8Ss9Tt0Uu1Vv2Ww3Xx4Yy5Zz";
            var string_length = 7;
            captchastring = '';
            for (var i = 0; i < 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 the element

                x.innerHTML = "valid";
                x.style.color = "#ff0000";

            }
            else {

                x = document.getElementById("demo");  // Find the element
                x.innerHTML = "Invalid Captcha. Try again";
                x.style.color = "#ff0000";
            }
        }

     </script>

This is my body of my 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 is happening currently? I mean, how is the application behaving?EDIT:You can try server side validation for example if you have a field to validate you can add a validation tag there.For example:

<input type="text" name="SampleTextBox" id="SampleTextBoxId"/>
 @Html.ValidationMessage("SampleTextBox", "*")

Then you go to the controller and add this kind of code:

if (!string.IsNullOrEmpty(SampleTextBox))
{
//Your Code.
}
  else
{
  ViewData.ModelState.AddModelError("SampleTextBoxId", "Text should not be empty.");
}

Use Model.IsValid as your condition to write your main code. Model.IsValid becomes False if ViewData.ModelState.AddModelError("SampleTextBoxId", "Text should not be empty."); is executed. This is a way to add validations. You can check for your valid/invalid captcha in your controller itself and throw error. For Example:

if (IsValidCaptcha(enteredCaptcha))
{
//Code
}
else
{
ViewData.ModelState.AddModelError("captchaId", "Enter valid captcha");
}

Lastly, add a validation summary to your page

@Html.ValidationSummary("Error Messages")

这篇关于如何在Asp.Net Mvc4检查验证码后,通过电子邮件ID值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:48