• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

How to use RequiredFieldValidor in C# ASP.NET???

Hi, I am trying to use Validation on a form of mine but when you click the Sign Up button I don't see it validating anything. The error message is "You cannot leave this field blank." but it does not appear any where it just takes me back to the Login tab.

Click the Sign Up tab:
http://auroriella.com/login.aspx

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="You cannot leave this field blank." 
            ValidationGroup="Group1" runat="server" ControlToValidate="firstname" />

Open in new window


I haven't written the script for logging in/signing up yet but I wanted to see that the validation worked when I left a field blank and tried to hit the submit button.  Does anyone know how to make this work?


login.aspx
0
FairyBusiness
Asked:
FairyBusiness
  • 5
  • 5
2 Solutions
 
NavneetSoftware EngineerCommented:
0
 
NavneetSoftware EngineerCommented:
is it this way
<%@Page Language="C#"%>
<html>
<body>

<form runat="server">

<asp:textbox id="txtTest" text="delete this" runat="server" />

<asp:RequiredFieldValidator id="valRequired" runat="server"
     ErrorMessage="* You must enter a value in the text box"
     ControlToValidate="txtTest"
     Display="dynamic">
*
</asp:RequiredFieldValidator>

<br />

<asp:button text="Validate" runat="server" />

<asp:ValidationSummary id="valSummary" runat="server"
     HeaderText="<b>The following errors were found:</b>"
     ShowSummary="true"
     DisplayMode="List" />


</form>

</body>
</html>

Open in new window


Or do post the HTML code
0
 
FairyBusinessAuthor Commented:
here is the html code:

        <form method="post" action="" runat="server">
            <article class="align-left">
            <label>First Name<span class="red">*</span></label>
            <asp:TextBox id="firstname" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Last Name<span class="red">*</span></label>
            <asp:TextBox id="lastname" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Email Address<span class="red">*</span></label>
            <asp:TextBox id="email" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Password<span class="red">*</span></label>
            <asp:TextBox id="password" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Re-enter Password<span class="red">*</span></label>
            <asp:TextBox id="repassword" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            </article>
            <article class="align-left">
            <label>Business Name<span class="red">*</span></label>
            <asp:TextBox id="business" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Employer ID Number<span class="red">*</span></label>
            <asp:TextBox id="eid" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Tax ID Number<span class="red">*</span></label>
            <asp:TextBox id="tid" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <label>Business Address<span class="red">*</span></label>
            <asp:TextBox id="address" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            <details class="align-left city">
                <label>City<span class="red">*</span></label>
                <asp:TextBox id="city" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            </details>
            <details class="align-left state">
                <label>State<span class="red">*</span></label>
                <asp:TextBox id="state" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            </details>
            <details class="align-left zip">
                <label>Zip<span class="red">*</span></label>
                <asp:TextBox id="zip" value="" class="form-text" ValidationGroup="Group1" runat="server" />
            </details>
            </article>
            <br />
            <asp:Button id="submit" text="Sign Up" class="form-submit" runat="server" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="You cannot leave this field blank." 
            ValidationGroup="Group1" runat="server" ControlToValidate="firstname" />
        </form>

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
FairyBusinessAuthor Commented:
yeah I have a book that just about says the same thing as that website link you gave me.  I think I get the basic concept of it, I'm just now sure how it works exactly, or what its supposed to do because it does not seem to being doing much for me.
0
 
NavneetSoftware EngineerCommented:
In Code-behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            // Validate intially to force *s to appear before the first round-trip
            Validate("Group1");
            Response.Write(IsValid.ToString());
        }

    }

Thanks!
0
 
FairyBusinessAuthor Commented:
Ahh ok! It works now but it puts the error message behind the submit button instead of the fields I left blank.  How can I get the error message to appear behind each field that was left blank?
0
 
NavneetSoftware EngineerCommented:
Hi!

You have to assign
            <label>First Name<span class="red">*</span></label>
            <asp:TextBox id="firstname" value="" class="form-text" ValidationGroup="Group1" runat="server" />
              <asp:RequiredFieldValidator ID="RequiredFieldValidator1" display="dynamic" ErrorMessage="First name required."
        ValidationGroup="Group1" runat="server"  ControlToValidate="firstname" />
            <label>Last Name<span class="red">*</span></label>
            <asp:TextBox id="lastname" value="" class="form-text" ValidationGroup="Group1" runat="server" />
                          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" display="dynamic" ErrorMessage="last Name required."
        ValidationGroup="Group1" runat="server"  ControlToValidate="lastname" />

Open in new window

0
 
FairyBusinessAuthor Commented:
so basically I have to have a separate <asp:RequiredFieldValidator> tag for each form field?  Is there a more simple way to validate in the c# code?

Like:

Validate("firstName", "lastName", "email")

??
0
 
NavneetSoftware EngineerCommented:

Hi!

There may be different third party controls
But basically either you assign individual Validatior
Or have one single Validator at the top Quoting "All Red Marked fields are mandatory"

Thanks!
0
 
FairyBusinessAuthor Commented:
ok thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now