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
FairyBusinessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Navneet.Net Full Stack DeveloperCommented:
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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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
Navneet.Net Full Stack DeveloperCommented:
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
Navneet.Net Full Stack DeveloperCommented:
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
Navneet.Net Full Stack DeveloperCommented:

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.