I was looking for a simple way to control the data entered in a form , I came to this solution that I can use six different validation server controls that are available for ASP.NET:
RequiredFieldValidator
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
ValidationSummary
but in order to use these controls I have to include runat=”server” in control tags and form tag.
the problem is when I want to specify a controller action in “action” attribute of “form” tag, It gives me the error:
Server tags cannot contain <% … %> constructs
Is there a way to use these controls and also call a controller action after submit?
here is my code:
<form name="register" action="<%: Url.Action("LogOn","Account") %>" runat="server">
<div class="form_row">
<label class="contact"><strong>Username:</strong></label>
<asp:TextBox id="UserName" runat="server" type="text" class="contact_input"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="Required!"
ControlToValidate="UserName">
</asp:RequiredFieldValidator>
</div>
<div class="form_row">
<label class="contact"><strong>Password:</strong></label>
<input id = "Password" name = "Password" type="password" class="contact_input" />
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="Required!"
ControlToValidate="Password">
</asp:RequiredFieldValidator>
</div>
<div class="form_row">
<div class="terms">
<input id = "RememberMe" type="checkbox" name="terms" />
Remember me
</div>
</div>
<div class="form_row">
<input type="submit" class="register" value="login" />
</div>
</form>
This looks like a very confused scenario. You describe an MVC project but you are using web form controls.
If you are using MVC, I would replace the .net controls with standard HTML controls and use data annotations for your validation. See here for information.
In MVC there should be no need to use
runat="server"for anything.