www.webdeveloper.com
Results 1 to 4 of 4

Thread: Form Validation C Sharp

  1. #1
    Join Date
    Aug 2010
    Posts
    34

    Form Validation C Sharp

    Hi there,

    I have a form and am trying to get the validation to work correcetly.

    The form :

    Code:
     <form id="ContactForm" runat="server" method="post">
            <ul class="ContactForm">
                    <li>
                        <label class="Label">
                            First Name:
                        </label>
                        <asp:TextBox CssClass="formField" MaxLength="50" ID="txtFirstName" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ErrorMessage="'First Name' must contain a valid entry"
                            ControlToValidate="txtFirstName" Display="None"></asp:RequiredFieldValidator>
                        <asp:Label ID="lblFirstNameRequired" runat="server"></asp:Label></li>
                    <li>
                        <label class="Label">
                            Surname:
                        </label>
                        <asp:TextBox CssClass="formField" MaxLength="50" ID="txtSurname" runat="server"></asp:TextBox><asp:RequiredFieldValidator
                            ID="rfvSurname" runat="server" ErrorMessage="'Surname' must contain a valid entry"
                            ControlToValidate="txtSurname" Display="None"><span class="error">required</span></asp:RequiredFieldValidator>
                        <asp:Label ID="lblSurnameRequired" runat="server"></asp:Label>
                    </li>
    				
    </ul>
    				
    				<asp:Button ID="btnSubmit" ToolTip="Submit form" CssClass="btnSubmitForm" runat="server"
                CausesValidation="true" Text="Submit" OnClick="btnSubmit_Click" />
    			
    </form>
    And the C# logic behind it.

    Code:
    protected void btnSubmit_Click(object sender, EventArgs e)
        {
    
            //Data Validation
    
            if (txtFirstName.Text.Length < 1)
            {
                lblFirstNameRequired.Text = "Required";
    
            }
            else
            {
                if (txtSurname.Text.Length < 1)
                {
                    lblSurnameRequired.Text = "Required";
                }


    On the submit button being clicked I would like the logic to validate the form.

    So far I ahve this, however I cant get it to work correctly.

    Any ideas or suggestions as to why and what I should be doing?

    Thanks/

  2. #2
    Join Date
    Sep 2009
    Posts
    163
    Just change the logic for btnSubmit_Click

    Code:
    if (txtFirstName.Text.Length < 1)
            {
                lblFirstNameRequired.Text = "Required";
    
          
    
    if (txtSurname.Text.Length < 1)
                {
                    lblSurnameRequired.Text = "Required";
                }
    
      }
    
    else 
    
    {
    
    type the code that has to followed or else leave it blank.
    }

  3. #3
    Join Date
    Aug 2010
    Posts
    34
    Hi developerguru, thanks for your response, however using the code you supplied I still have no joy

    Thanks.

  4. #4
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Should be able to tap into the IsValid property for the page:

    Code:
    if(!IsValid){
    //send your email
    }
    The rest should be handled internally by .NET framework.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles