在服务器端验证 [英] Validation on server side

查看:81
本文介绍了在服务器端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的下面的代码中,每当我在密码文本框中输入值(即txtPassword)时,当我在下一个文本框中输入值时,该值将被删除。

我调试了我的代码但仍未获得错误。



In my following code whenever I am entering value in my password textbox (i.e txtPassword) the value gets erased when i my entering value in next textbox.
I debugged my code but still not getting the error.

protected void Page_Load(object sender, EventArgs e)
    {
        txtSecQue.Visible = false;
        lblUser.Visible = false;
        //txtUsername.Focus();
        lblEmailID.Visible = false;
        lblQue.Visible = false;
        lblPassword.Visible = false;
    }
    protected void ddlSecQue_SelectedIndexChanged(object sender, EventArgs e)
    {
        
            if (ddlSecQue.SelectedItem.Text == "Other")
            {
                txtSecQue.Visible = true;
                txtSecQue.Focus();
            }
            else if (ddlSecQue.SelectedItem.Text=="----Select----")
            {
                lblQue.Visible = true;
            }
            else
            {
                txtAnswer.Focus();
            }
        
    }
    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        //to check length of the string
        string username=txtUsername.Text;
        if (username.Length<5 || username.Length>15)
        {
            lblUser.Text = "Username must be between 5 to 15 characters";
            lblUser.Visible = true;
            txtUsername.Focus();
            txtUsername.Text = "";
        }
        else
        {
            txtPassword.Focus();
        }

        string select = "Select * from Reg_Info where Username='"+txtUsername.Text+"'";
        SqlCommand cmd = new SqlCommand(select, con);
        SqlDataReader sdr;

        con.Open();
        sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            lblUser.Visible = true;
            txtUsername.Text = "";
            txtUsername.Focus();
        }
        else
        {
            txtPassword.Focus();
        }
        con.Close();
    }
    protected void txtEmailID_TextChanged(object sender, EventArgs e)
    {
       
        string select = "Select * from Reg_Info where EmailId='" + txtEmailID.Text + "'";
        SqlCommand cmd = new SqlCommand(select, con);
        SqlDataReader sdr;

        con.Open();
        sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            lblEmailID.Visible = true;
            txtEmailID.Text = "";
            txtEmailID.Focus();
        }
        con.Close();
    }
    protected void txtPassword_TextChanged(object sender, EventArgs e)
    {
        string password = txtPassword.Text;
        if (password.Length<7 || password.Length>15)
        {
            lblPassword.Visible = true;
            txtPassword.Text = "";
            txtPassword.Focus();
        }
        else
        {
            txtRePassword.Focus();
        }
    }







<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Registeration.aspx.cs" Inherits="Registeration" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style2
        {
            height: 23px;
        }
        .style3
        {
            height: 23px;
            width: 252px;
        }
        .style4
        {
            width: 252px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:TabContainer ID="TabContainer1" runat="server" Height="500" Width="1000" 

        ActiveTabIndex="0">
    <asp:TabPanel ID="TabLogin" runat="server">
    <HeaderTemplate>Login Details</HeaderTemplate>
        <ContentTemplate><table class="style1"><tr><td class="style3">Username :</td><td class="style2"><asp:TextBox 

                ID="txtUsername" runat="server" AutoPostBack="True" 

                ontextchanged="txtUsername_TextChanged"></asp:TextBox> <asp:RequiredFieldValidator 

                ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtUsername" 

                ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
             <asp:Label ID="lblUser" runat="server" Text="Username already exist"></asp:Label>
            </td></tr><tr><td class="style4">Password :</td><td><asp:TextBox ID="txtPassword" 

                    runat="server" TextMode="Password" AutoPostBack="True" 

                    ontextchanged="txtPassword_TextChanged"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 

                    ControlToValidate="txtPassword" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:Label ID="lblPassword" runat="server" Text="5 to 15"></asp:Label>
                </td></tr><tr><td class="style4">Re-Type Password :</td><td><asp:TextBox ID="txtRePassword" runat="server" TextMode="Password"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 

                    ControlToValidate="txtRePassword" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:CompareValidator ID="CompareValidator1" runat="server" 

                    ControlToCompare="txtPassword" ControlToValidate="txtRePassword"

                    ErrorMessage="CompareValidator"></asp:CompareValidator>
                </td></tr><tr><td class="style4">Email ID :</td><td><asp:TextBox 

                ID="txtEmailID" runat="server" Width="200px" AutoPostBack="True" 

                    ontextchanged="txtEmailID_TextChanged"></asp:TextBox> <asp:RequiredFieldValidator 

                    ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtEmailID" 

                    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 

                    runat="server" ControlToValidate="txtEmailID" 

                    ErrorMessage="RegularExpressionValidator" 

                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                 <asp:Label ID="lblEmailID" runat="server" Text="EmailID already exist"></asp:Label>
                </td></tr><tr><td class="style4">Security Question :</td><td><asp:DropDownList 

                    ID="ddlSecQue" runat="server" 

                            onselectedindexchanged="ddlSecQue_SelectedIndexChanged" 

                    AutoPostBack="True" ><asp:ListItem>----Select----</asp:ListItem><asp:ListItem>Where you where born</asp:ListItem><asp:ListItem>What is your cousin name ?</asp:ListItem><asp:ListItem>What is your first grade teacher name ? </asp:ListItem><asp:ListItem>Other</asp:ListItem><asp:ListItem></asp:ListItem></asp:DropDownList>  <asp:TextBox ID="txtSecQue" runat="server"></asp:TextBox> <asp:Label 

                    ID="lblQue" runat="server" Text="Please Select one option"></asp:Label>
                </td></tr><tr><td class="style4">Answer :</td><td><asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 

                    ControlToValidate="txtAnswer" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                </td></tr><tr><td colspan="2">               <asp:Button ID="btnReg" runat="server" Text="Register" />   <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td></tr></table></ContentTemplate>
    </asp:TabPanel>

    <asp:TabPanel ID="TabPersonalInfo" runat="server">
    <HeaderTemplate>Personal Information</HeaderTemplate>
        <ContentTemplate><table class="style1"><tr><td>First Name :</td><td> </td></tr><tr><td>Last Name :</td><td> </td></tr><tr><td>Address Street1 :</td><td> </td></tr><tr><td>Address Street2 :</td><td> </td></tr><tr><td>City :</td><td> </td></tr><tr><td>State :</td><td> </td></tr><tr><td>Date of Birth :</td><td> </td></tr><tr><td>Mobile No :</td><td> </td></tr><tr><td>Alternate No :</td><td> </td></tr><tr><td>Gender :</td><td> </td></tr><tr><td 

                colspan="2">         <asp:Button ID="btnReg0" runat="server" 

                Text="Register" />  <asp:Button ID="btnCancel0" runat="server" 

                Text="Cancel" /></td></tr></table></ContentTemplate>
    </asp:TabPanel>
    </asp:TabContainer>
</asp:Content>





I am using autoPostback to check each textbox

Tell me where I am going wrong



Thank You



I am using autoPostback to check each textbox
Tell me where I am going wrong

Thank You

推荐答案

You are setting the txtPassword to an empty string on the TextChanged Event. That is why it is blanking out. It appears you are doing that with all of the text fields.
You are setting the txtPassword to an empty string on the TextChanged Event. That is why it is blanking out. It appears you are doing that with all of the text fields.


When you enter the value in txtEmailID and move the mouse out , the page postbacks because you have set the TextChanged event and the Autopostback=true for this textbox. And passwords are cleared by the browser on postbacks for security reasons...





And why in the world , you have password fields before email ID ???
When you enter the value in txtEmailID and move the mouse out , the page postbacks because you have set the TextChanged event and the Autopostback=true for this textbox. And passwords are cleared by the browser on postbacks for security reasons...


And why in the world , you have password fields before email ID ???


ur checking password strength on server side by textchange event that’’s why autopostback is ocured except that add RegularExpressionValidator

for example

ur checking password strength on server side by textchange event that''s why autopostback is ocured except that add RegularExpressionValidator
for example
<asp:RegularExpressionValidator ID="revPassCheck" runat="server" ErrorMessage="Password lenght must be between 6 to 15 characters."

            ControlToValidate="txtPassword" ValidationExpression="[\s\S]{7,15}"></asp:RegularExpressionValidator>


这篇关于在服务器端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆