asp:RequiredFieldValidator and radiobuttons - asp.net

This is my first question in stackoverflow.com.
I am working on a school project which I have to validate the input from the user. Everytime the page load, it give the server error message. Please see the codes, and the error message after.
<div>
<table>
<td>
<asp:RadioButton ID="RadioButton1" runat="server"></asp:RadioButton>
<asp:RequiredFieldValidator ID="validateCheck" runat="server" ControlToValidate="RadioButton1" ErrorMessage="Please Enter" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</table>
</div>
Server Error in '/' Application.
Control 'RadioButton1' referenced by the ControlToValidate property of 'validateCheck' cannot be validated.

The RequiredFieldValidator does not validate a RadioButton. However, you can use the RadioButtonList control instead (validated by RequiredFieldValidator).

The asp:RadioButton do not support validation, instead of RadioButton use a RadioButtonList:'
<form id="form1" runat="server">
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="RadioButtonList1"></asp:RequiredFieldValidator>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
</asp:RadioButtonList>
</form>

Related

Validation Not Firing On TextBox

So, basically I have multiple TextBoxes on my page and some of them have validations with RequiredFieldValidator.
I am using OnTextChanged event on txtMobileNo TextBox Field to fetch the data from database.
If the number exists in the database the the rest of my TextBoxes filled from server side.
Now, I am facing here a problem: my MobileNo Text Field skips the RegularExpressionValidator validation even if it is wrong.
I want to redirect to server side only if my number matches the validation logic.
<div class="col-md-4">
<asp:Label ID="lblfortxtMobileNo" runat="server" Text="Mobile No"></asp:Label>
<asp:TextBox ID="txtMobileNo" runat="server" AutoPostBack="true" OnTextChanged="txtMobileNo_TextChanged" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqfvtxtMobileNo" runat="server" ErrorMessage="*" ControlToValidate="txtMobileNo" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Please Enter Valid Number"
ControlToValidate="txtMobileNo"
Display="Dynamic"
ForeColor="Red"
ValidationExpression="^([0-9]{10})$"
></asp:RegularExpressionValidator>
</div>
<div class="col-md-4">
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtName" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
</div>
<div class="col-md-4">
<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />
</div>
When I am set CauseValidation = "true" the result is somewhat what I want .
But it checks all the other RequiredFieldValidator validation as well before calling OnTextChanged function.
I just want to check Regular Expression Validation at this point for txtMobileNo and based on the result wants to display error.
Cross check your regex id it is look like an duplicated id here.

Required Field Validation not working in asp.net

I have required field validation control for a radiobutton list. So if no values are selected then it gives me a error which is fine. But when i redo select something and click the button then it does't not fires the server event of the button. Once i have the validation erro then whatever i do it disable the server side event.
any ideas why is it happening my code
<div id="studysub_popul" runat="server" visible="false">
<asp:Label ID="lbl_rdb_study_popul" runat="server"
CssClass="questions"
Text="2.Select your study subjects">
</asp:Label>
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged">
<asp:ListItem>Individuals</asp:ListItem>
<asp:ListItem>Population</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="rdb_study_popul"
Display="Dynamic"
ErrorMessage="Study Subject is required"
ValidationGroup="StudySubject">
</asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn_s_section" runat="server"
OnClick="btn_studysubject_section_Click"
Text="Next" ValidationGroup="StudySubject"
Visible="false" />
</td>
You should add a validating group to the RadioButtonList definition too.
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged"
ValidationGroup="StudySubject">

Validation called on controls from different validation groups

I have 2 buttons and other controls in a page. Button1 has validation group Validation1 and Button2 has Validation2. When I click Button1, controls with Validation2 are validated, which is not supposed to happen. How can I avoid this?
Have you putted validation group to all the controls.You need to give validation groups controls to all the fields including validator controls. Just like following.
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="firstTextBox" ValidationGroup="vg1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="requiredFirst" ControlToValidate="firstTextBox" ValidationGroup="vg1"></asp:RequiredFieldValidator>
<asp:Button runat="server" ValidationGroup="vg1" Text="SaveData" ID="btnSaveFirst"/>
</div>
<div>
<asp:TextBox runat="server" ID="secondTextBox" ValidationGroup="vg2"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="requiredSecond" ControlToValidate="secondTextBox" ValidationGroup="vg2"></asp:RequiredFieldValidator>
<asp:Button runat="server" ValidationGroup="vg2" Text="SaveData" ID="btnSaveSecond"/>
</div>
</form>

Loading gif on login control

I would like my login control to have a "Loading..." gif shown when a user logs in.
In order to have my login control display how I wanted, I converted it to a template. I then followed the guidance seen here: here and here to show the gif during a postback.
I'm having a couple of problems now though. When I try to sumbit an invalid login, the page does as it's supposed to: it shows the loading gif, and then the gif goes away after the login is determined invalid. But now even when I enter a valid login it says it's invalid. Did I mess something up when I edited the login template?
My other problem is that I have the "keepLogged" checkbox stylized. The style is being added by JQuery in the head. It shows it properly when you first go to the page, but after the postback, when the login is shown to be invalid, the style isn't applied and it's a normal checkbox.
Here's the code:
<script>
$(document).ready(function () {
$("input[type=checkbox]").addClass("mini-switch");
});
</script>
<form id="form1" runat="server" class="form with-margin" name="login-form">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updateLogin" runat="server" DefaultButton="mainLogin$LoginButton">
<ContentTemplate>
<asp:Login ID="mainLogin" runat="server" RenderOuterTable="False" onloginerror="mainLogin_LoginError">
<LayoutTemplate>
<div class="block-header">Please login</div>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server"
ValidationGroup="mainLogin" CssClass="message error no-margin"
DisplayMode="List"/>
<input type="hidden" name="a" id="a" value="send">
<p class="inline-small-label">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"><span class="big">User name</span></asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass='full-width'></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required." ValidationGroup="mainLogin"
Display="None" />
</p>
<p class="inline-small-label">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"><span class="big">Password</span></asp:Label>
<asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass='full-width'></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
ControlToValidate="Password" ErrorMessage="Password is required."
ToolTip="Password is required." ValidationGroup="mainLogin"
Display="None" />
</p>
<asp:Button ID="LoginButton" runat="server" CommandName="Login"
Text="Log In" type="button"
ValidationGroup="mainLogin" onclick="LoginButton_Click" CSSClass='button float-right'/>
<p class="input-height">
<asp:CheckBox ID="keepLogged" runat="server" Checked="True"/>
<asp:Label ID="Label1" runat="server" Text="Keep me logged in" AssociatedControlID="keepLogged" CssClass="inline"></asp:Label>
</p>
</LayoutTemplate>
</asp:Login>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<img alt="" src="images/lock.gif" /> <strong>Logging In...</strong>
</ProgressTemplate>
</asp:UpdateProgress>
</form>
Thanks for any help.
I don't think that the built-in ASP.NET login control will work properly inside of an UpdatePanel. There are other ways to achieve the same effect, though. There's an article here that should get you started.

Validation Summary does not render correctly

Has anyone ever seen this and does anyone know how to fix it ...
The validation Summary control seems to be rendering this :
error message
<br>
error message
<div style="display: none;" id="summaryID">
</div>
Correct me if i'm wrong here but shouldn't my errors be inside that div ... hense the reason you can put a cssclass on the control server side using CssClass="whatever" ?
Or did i miss something?
EDIT :
Just to verify ... this is what i expect it to render :
<div style="display: none;" id="summaryID">
error message
<br>
error message
</div>
EDIT 2 :
Server side markup that produces this is ...
<asp:ValidationSummary ID="ui_ValidationSummary" runat="server" />
...
Loads of controls but here's an example (don'twant to over complicate things) :
...
<asp:TextBox ID="ui_txtClientDOB" runat="server" />
<asp:RangeValidator ID="ui_RangeValidator_DOB" runat="server" ControlToValidate="ui_txtClientDOB" ErrorMessage="DOB is not valid" MinimumValue="1900/01/01" Type="Date" ForeColor="Red">*</asp:RangeValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ui_txtClientDOB" ErrorMessage="DOB is missing" ForeColor="Red">*</asp:RequiredFieldValidator>
If either validator on this textbox has reason to fail the validation the result is as discussed, an empty div tag with an error next to it.
Likely the div #summaryID is where client side validation errors are show. Use a tool like firebug to see this as you can't do a view source on javascript filled markup. Otherwise, turn off client side validation and do a postback.
Edit
My guess is that you are mixing up the error message that's displayed with the validation control itself instead of inside the validation summary. If you don't supply a text property to the validation controls it will use the error message. Try the following (take away the text property and you'll see what I mean):
<asp:ValidationSummary ID="val" runat="server" CssClass="test"
DisplayMode="List" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" Text="*"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator" Text="*"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />

Resources