Required Field Validator on FileUpload Control - asp.net

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.

Related

ASP .NET CompareValidator control locks everything

I have a form with a field that has a asp:CompareValidator control which checks if the text in the input is of type Integer.
The problem is that the form has some other buttons to navigate to other pages, so when the CompareValidator triggers it locks those buttons until the input in the controled field is Integer although it's not necessary to navigate.
Here's the code:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>
Is this the expected behaviour of the asp:CompareValidator control?
You can specify a ValidationGroup in your button and in your CompareValidator. Then the CompareValidator will be used only for the corresponding ValidationGroup:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" ValidationGroup="form" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" ValidationGroup="form" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>

asp.Net ValidationGroup validating correctly except on Enter key

I've seen a variety of questions here that are very close to this issue but none which seem to fit this specific scenario.
Description:
I have ID and Password fields with a Log On button in the Master page. These are in the "LoginValidationGroup" and they work fine.
Now, in the Default.aspx, I have an email TextBox and submit button that are in the "NotifyMeValidation" group, and they also work, BUT not if you hit the enter key rather than the submit button. The Submit button works fine - Enter key ... not so much.
The Enter key causes the validation to occur on the LoginValidationGroup, even though the TextBox is set to CausesValidation="true", and it is in the NotifyMeValidation group.
I guarantee people are going to enter an email in that box and hit Enter. I would!!! And when they do, they're going to see a callout balloon at the top telling them the User ID is required.
What's my error here? If I need to post the actual code I can do so.
Actually, let me just go ahead and do that.
From the Default.aspx:
<asp:RequiredFieldValidator
runat="server"
ValidationGroup="NotifyMeValidation"
ID="EmailRequiredValidator"
ControlToValidate="SenderEmailTextBox"
ErrorMessage="Email is Required"
Display="None" />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
PopupPosition="Left"
ID="EmailRequiredValidatorExtender"
Width="185px"
TargetControlID="EmailRequiredValidator"
WarningIconImageUrl="/Images/warning.gif" />
<asp:Label ID="SenderEmailLabel" runat="server" ssociatedControlID="SenderEmailTextBox" EnableViewState="false" Text="Email:"></asp:Label>
<asp:TextBox ID="SenderEmailTextBox" runat="server" ValidationGroup="NotifyMeValidation" Width="350" BackColor="#cccccc" CausesValidation="true"></asp:TextBox>
<br /><br />
<asp:Button ID="SubmitButton" runat="server" ValidationGroup="NotifyMeValidation" EnableViewState="false" CssClass="submit" Text="Send" CausesValidation="true" OnClick="btnSubmit_Click" />
From the Master page:
<asp:Label CssClass="LoginHeading" ID="UserNameLabel" runat="server" AssociatedControlID="UserNameTextbox">UserName: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="UserNameTextbox" runat="server" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Label CssClass="LoginHeading" ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextbox">Password: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="PasswordTextBox" runat="server" TextMode="Password" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Button CssClass="LoginHeading" ID="LoginButton" runat="server" Text="Button" CommandName="Login" ValidationGroup="LoginValidation" CausesValidation="True" onclick="LoginButton_Click" />
And the Master page validators...
<asp:RequiredFieldValidator runat="server" ID="UIDRequired"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A User ID is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="UIDValidationExtender"
PopupPosition="Left"
Width="185px"
TargetControlID="UIDRequired"
WarningIconImageUrl="/Images/warning.gif" />
<asp:RequiredFieldValidator runat="server" ID="PWRequired"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A password is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="PWValidationExtender"
PopupPosition="Left"
TargetControlID="PWRequired"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
Display="None"
ValidationGroup="LoginValidation"
ID="CustomUserExistsValidator"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Invalid UserID!</b><br />User ID doesn't exist. Please try again.<br />"
OnServerValidate="CustomCheckUserExists"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserIDCalloutExtender"
TargetControlID="CustomUserExistsValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomPWValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
ErrorMessage="<b>Invalid Password!</b><br />Please try again.<br />"
OnServerValidate="CustomValidatePassword"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="PWCalloutExtender"
TargetControlID="CustomPWValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomUserApprovedValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Approval Error!</b><br />This UserID exists, but is not yet approved.<br />"
OnServerValidate="CustomCheckUserApproved"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserApprovedCalloutExtender"
TargetControlID="CustomUserApprovedValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
Try adding an <asp:Panel> around each set of controls and setting the DefaultButton property to the ID of the button you want to click when the users hits Enter.
Just to explain what is happening here, the aspx's default button is being fired when you press enter on the textbox. To handle this you could you could fire some jQuery code when the enter key is clicked on the textbox, this code will then perform the click on the specific button that you require. Check this

asp.net: Postback disabled after validation failed

After validation failed, I have a problem with controls (dropdownlist or button) that should cause a new postback. I will try to explain it clearly...
The purpose of my page is to save fives dates in a database. The page has the following controls:
Five textboxes, each one containing a date
A reset button (CausesValidation=false) to restore a default date in one of the 5 textboxes
A dropdown list (AutoPostback=true, CausesValidation=false) of predefined templates that applies 5 dates to the 5 textboxes
A button to save the dates to the database
The textboxes can be edited manually. So, when I click the Save button, if the format of the dates is not valid, the validation fails and the save is aborted. The problem is just after that. If I click on the Reset button or select an item in the dropdownlist, the postback is not triggered. If I try again, then it works. Is there a way to make it work the first time after the first validation failed? I tried deactivating the validation on the client-side when changing the selection in the dropdownlist but the postback still does not occur.
Here is the relevant part of the code:
<asp:DropDownList ID="cboScheduleTemplates" runat="server" AutoPostBack="true" CausesValidation="false" />
<asp:TextBox ID="txtDateDelivery1" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery1" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery1" Text="*" />
<asp:TextBox ID="txtDateYearbookQuantity" runat="server" />
<asp:RegularExpressionValidator ID="revDateYearbookQuantity" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateYearbookQuantity" Text="*" />
<asp:TextBox ID="txtDateDelivery2" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery2" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery2" Text="*" />
<asp:TextBox ID="txtDatePersonalizations" runat="server" />
<asp:RegularExpressionValidator ID="revDatePersonalizations" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDatePersonalizations" Text="*" />
<asp:TextBox ID="txtDateDelivery3" runat="server" />
<asp:Button ID="btnSetDefaultDelivery3" runat="server" ValidationGroup="Schedule" CausesValidation="false" />
<asp:RegularExpressionValidator ID="revDateDelivery3" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery3" Text="*" />
<asp:Button ID="btnSaveSchedule" runat="server" CssClass="btnAction" Text="Save" ValidationGroup="Schedule" />
<asp:ValidationSummary ID="validationSummarySchedule" runat="server" ValidationGroup="Schedule" DisplayMode="List" />
As suggested in this post, the problem comes from calls to Page_ClientValidate. So I wrapped the client function like this and the problem went away:
function DoPageClientValidate(validationGroupName)
{
var result = Page_ClientValidate(validationGroupName);
Page_BlockSubmit = false;
return result;
}

How to limit validation to a particular trigger?

Is there any way to limit the validation of a validator control to a particular event or trigger? Say, I want my validator1 to be activated only when button1 was clicked, and validator2 to be activated only when button2 was clicked. How can I do this in asp.net?
ValidationGroups are what you're looking for(ASP.NET >= 2.0)
Validation groups allow you to organize validation controls on a page
as a set. Each validation group can perform validation independently
from other validation groups on the page.
<asp:textbox id="AgeTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation="true"
validationgroup="PersonalInfoGroup"
runat="Server" />
<br /><br />
<asp:textbox id="CityTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="CityTextBox"
validationgroup="LocationInfoGroup"
errormessage="Enter a city name."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button2 is clicked, only validation
controls that are a part of LocationInfoGroup
are validated.-->
<asp:button id="Button2"
text="Validate"
causesvalidation="true"
validationgroup="LocationInfoGroup"
runat="Server" />

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">

Resources