Required field validator not responding or firing - asp.net

I have several text boxes on my website, each with a required field validator, and a submit button on the bottom. The submit button works whether or not I have entered values into the text boxes. Below is the code:
<asp:TextBox ID="TextBoxName" ValidationGroup="textgroup" runat="server"
ToolTip="Insert the Pokemon's name" CssClass="upper-case" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ValidationGroup="textgroup" Text="Names only please" ControlToValidate="TextBoxName"
ValidationExpression="^[a-zA-Z]*$" CssClass="required-field" />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="textgroup"
runat="server" Text="Required Field" ControlToValidate="TextBoxName"
CssClass="required-field" />
<br />
Here is the submit button:
<asp:Button ID="Button1" runat="server" Text="Insert Card" ValidationGroup="textgroup" />
And the code-behind for the submit button:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Page.Validate()
CardCollectionDataSource.Insert()

Related

ASP.NET Custom validator for unique entry is not working

I am trying to implement a custom validator to comapare the values in database so that the entry is unique - in my registration page.
This is my aspx page
<h4> Sign up </h4>
<div class="">
<div class="status">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableViewState="false"
ValidationGroup="EmployerRegister" HeaderText="Sorry we could not process request because:"
CssClass="error" />
</div>
<p class="input-block last">
<label for="<%=uxEmployerName.ClientId %>">
<strong>Company Name</strong> (required)
</label>
<asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10"
ValidationGroup="EmployerRegister" />
<asp:RequiredFieldValidator ID="uxEmployerNameValReq" runat="server" ControlToValidate="uxEmployerName"
ErrorMessage="You must enter Company Name." EnableClientScript="true" Display="Dynamic"
ValidationGroup="EmployerRegister" />
</p>
<p class="input-block last">
<label for="<%=uxEmail.ClientId %>"> <strong>Your Email</strong> (required) </label>
<asp:TextBox ID="uxEmail" runat="server" CssClass="uxemail" TabIndex="16" ValidationGroup="EmployerRegister" />
<asp:RequiredFieldValidator ID="uxEmailValReq" runat="server" ControlToValidate="uxEmail"
ErrorMessage="You must enter Email." EnableClientScript="true" Display="none"
ValidationGroup="EmployerRegister" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="The 'Email Address' you have entered does not appear to be a valid address."
ControlToValidate="uxEmail" EnableViewState="False" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="EmployerRegister" Display="None" />
<asp:CustomValidator ID="uxEmailValUnique" runat="server" OnServerValidate="uxEmailValUnique_ServerValidate" ErrorMessage="It appears that you already have an account with us."
ControlToValidate="uxEmail" EnableViewState="False" ValidationGroup="EmployerRegister"
Display="None" />
</p>
<p class="input-block last">
<asp:LinkButton runat="server" ID="uxRegisterButton" Text="Submit" CausesValidation="true"
ValidationGroup="EmployerRegister" CssClass="button" TabIndex="18" />
</p>
</div>
The server side code in vb.net page is
Private Sub uxEmailValUnique_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles uxEmailValUnique.ServerValidate
args.IsValid = Not LocalHelper.EmailExists(uxEmail.Text)
End Sub
the function in LocalHelper file is
Shared Function EmailExists(email As String) As Boolean
Return DB.GetInteger("select count(id) from [user] where deleted = 0 and active = 1 and email = #email", DB.SIP("email", email)) > 0
End Function
but the custom validator is not working. its taking duplicate email address and not giving any error message. My database is connected properly.
what is wrong in this code/ how to fix this.
Create an event handler for the CustomValidator, that is missing in your html, control's ServerValidate event like this:-
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
OnServerValidate="ServerValidation"
runat="server"/>
If this doesn't work then on submit event try this:-
Page.Validate()
If Page.IsValid Then
'your code
End If
Your Custom Validator ServerValidate code
Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs)
args.IsValid = False
End Sub
Hope this will help you.

Required Field Validator on FileUpload Control

I have a form in which I have two textboxes and one fileupload control, I am using required field validator on one textbox and on Fileupload control, When I am clicking the submit button, its disabling the fileupload control and not showing any validation for it.
I also have second button for Cancel, clicking on which redirects to previous page, when I am clicking this button its also disabling the fileuploadcontrol.
Below is my code
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="validate" runat="server"
ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtSubj" runat="server" ></asp:TextBox><asp:RequiredFieldValidator
ID="rfvSubject" ControlToValidate="txtSubj" runat="server" Display="Dynamic"
EnableClientScript="true" ErrorMessage="* required" ValidationGroup="validate" />
<asp:Button ID="btnupload" runat="server" Text="Send" ValidationGroup="validate"
OnClick="btnupload_Click">
<asp:Button ID="btncancel" runat="server" Text="Cancel" OnClick="btncancel_Click"
/>
Try using no ValidadationGroup:
<asp:FileUpload ID="fupDocument" runat="server" Width="100%" />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="* Required Field" ControlToValidate="fupDocument">
</asp:RequiredFieldValidator>
I tried and work it.

asp.net required field validator

A required field validator should fire only after clicking a submit button
<asp:RequiredFieldValidator id="req" controltovalidate="txtphone" errormessage="please enter the details">
</asp:RequiredFieldValidator>
Youre missing runatserver on your code
Check this:
<form id="form1" runat="server">
Phone Number:<br />
<asp:TextBox runat="server" id="txtphone" />
<asp:RequiredFieldValidator runat="server" id="req" controltovalidate="txtphone" errormessage="Please enter your phone number!" />
<br /><br />
<asp:Button runat="server" id="btnSubmitForm" text="Ok" />
</form>
Regards
If you are using a button field try by applying Validation Group Property in both the validator and Button as follows
<asp:RequiredFieldValidator id="req" runate="Server" controltovalidate="txtphone" errormessage="please enter the details" ValidationGroup="g">
<asp:button id="btn" runat="server" validationgroup="g">

asp.net CompareValidator not running as expected on submit

I'm trying to get a CompareValidator to work but having a few problems. If the submit button does not run a function it works fine, but the validator does not work if there is a function attached to the button.
Here's the code.
Sub myGo(sender As Object, e As EventArgs)
response.redirect("http://www.google.co.uk")
End Sub
and
<form runat="server">
<asp:TextBox id="txt1" runat="server" /> = <asp:TextBox id="txt2" runat="server" /> <asp:Button OnClick="myGo" Text="not working with onclick" runat="server" /> <asp:Button Text="working button" runat="server" />
<br />
<asp:CompareValidator EnableClientScript="false" id="compval" Display="dynamic" ControlToValidate="txt1" ControlToCompare="txt2" Type="String" Text="Validation Failed!" runat="server" />
</form>
any ideas?
The validator should work fine, but you probably ought to perform the redirect only if the page is valid:
Sub myGo(sender As Object, e As EventArgs)
If Page.IsValid Then
Response.Redirect("http://www.google.co.uk")
End If
End Sub
Use ValidationGroup="...."

AJAX ModalPopup update panel contents based on change in panel controls

I have an AJAX Modal Popup panel that contains a RadioButtonList, 2 labels and 2 DropDowns. I want to update the Labels and DropDowns when a radio button is selected. My attempt at this posts back which causes the ajax popup to disappear.
aspx called on image click:
<asp:Panel ID="pnlModalContainer" runat="server">
<asp:RadioButtonList ID="rblTest" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblTest_SelectedIndexChanged">
<asp:ListItem Text="Test 1" Value="1" Selected="True" />
<asp:ListItem Text="Test 2" Value="2" />
</asp:RadioButtonList>
<br />
<asp:Label ID="lblFoo" Text="Foo" />
<asp:Label ID="lblBar" Text="Bar" />
<asp:DropDownList ID="ddlDogs" runat="server" DataSourceID="odsDogs" DataTextField="Dog" DataValueField="DogID" />
<asp:DropDownList ID="ddlCats" runat="server" DataSourceID="odsCats" DataTextField="Cat" DataValueField="CatID" />
</asp:Panel>
Code Behind (vb.net):
Protected Sub rblTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rblTest.SelectedIndexChanged
' ???
' Make it change lblFoo.Text and lblBar.Text as well as the DataSource for the DDLs
End Sub
You need to add an UpdatePanel within pnlModalContainer.

Resources