field validation on publishing the website - asp.net

On my development PC, I created simple form with 3 text boxs and a submit button. I added validation to the text boxes and time to last text box.
<asp:TextBox
ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox1"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox2"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<br />
<asp:TextBox
ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox3"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
(and added now() to textbox3)
On submitting the form, when running on local host (my development PC), the validation executes fine with no post back (means I don’t see changes on time on text box3). However, when I upload all the files to the server, upon submitting I see validation but it happens after post back which I see a new time on textbox3. Also I get this JavaScript error:
<input type="submit" name="Button1" value="Button"
onclick="javascript:WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(
"Button1",
"",
true,
"", "",
false, false))" id="Button1" />
What am I doing wrong? Should the solution be under release or debug?

Related

How to make validator's message appear nearby another control?

I have a validator with a bound text field (it's set via ControlToValidate). How can I make the validator's error message appear nearby another control (a label above this text field)?
Just put the validator control nearby the control where you want to show the message i.e. wherever you want to show the message just put the validator control there.
In the following example I am showing validation message near some other control not near the text box.
<form id="form1" runat="server">
<asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
<asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
<br />
<asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
<asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
<asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
Display="Dynamic" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
ValueToCompare="-1"></asp:CompareValidator>
<br />
<asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="Dynamic" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
</form>
Hope this helps 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.

Required and RegularExpression Validators taking up space

I'm having issues with asp.net's field validators taking up space on my page. I've searched around, and documentation says to use Display="Dynamic" to keep the validators from taking up space. When I use this however, the error messages are always displayed.
What am I doing wrong?
I just want the error messages to display when the user either clicks the "Save" button, or loses focus on a textbox. And I don't want the validators to take up space.
<p>Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGgroup="Email"
ErrorMessage="Please enter an email"
ControlToValidate="TextBoxEmail" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
ValidationGroup="Email"
ControlToValidate="TextBoxEmail"
ErrorMessage="Please enter valid email"
runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" />
<p>Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:LinkButton ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" />
Only thing I notice in your code is you have a typo in ValidationGgroup="Email". It should be ValidationGroup="Email"
Other than that, Display="Dynamic" works for me.
After clicking, submit button -
<p>
Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Email"
ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email"
ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" />
<p>
Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TextBoxEmail"
ControlToValidate="TextBoxEmail2" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Must match." ValidationGroup="Email"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="Email"
ErrorMessage="Please enter an confirm email" ControlToValidate="TextBoxEmail2" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" ValidationGroup="Email" runat="server" Text="Save"
OnClick="linkbuttonSave_Click" />
You can set the property Display to "Dynamic" or "none", the second one will cause the Error message to be displayed only in the validation summary.
I got it to work great by deleting the "ValidationGroup=.." . Havent looked what up what this does but it works great without it and not sure why it would be needed (given the validator controls have the control id of the email textbox)

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

html5 required is not allowing button click action to fireup

I have an input page where i want to create some required fields and some aren't.
That means that at the first part of the form, you can select the date by pressing a button to show a calender to select the desired date, adding users (by pressing a button) and then you add the required text and finally a submit button.
Right now, when I put the required attribute to the input tag, the first two buttons don't work. They don't fire up and it writes that first I need to insert the required items.
How can I remove the connection between those specific buttons and the required inputs?
Thanks, Arkadi.
Code:
<h3> Create New Project</h3>
Users Added: <asp:TextBox ID="TextBox1" runat="server"
ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblAdded" runat="server" Text="Label" >User Was Already Added</asp:Label>
<asp:Label ID="lblNotFound" runat="server" Text="Label" >User Was Not Found</asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"></asp:CheckBoxList>
Add User<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /><br />
Project Name: <input ID="txtName" runat="server" required="required" />
Hand Over Date: <asp:TextBox ID="txtHandOverDate" runat="server" ReadOnly="true"></asp:TextBox>
<asp:Button ID="btnSelectDate" runat="server" Text="SelectDate"
OnClick="btnSelectDate_Click" />
<asp:Calendar ID="Calendar1" runat="server" Visible="False"
OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
Hand Over Form: <input ID="txtHandOverForm" runat="server" required="required"/>
Subject: <input ID="txtSubjectField" runat="server" required="required" />
Project Porpuse <input ID="txtPorpuse" runat="server" required="required" />
Perimiters<input ID="txtPerimiters" runat="server" required="required" />
Comments <textarea id="txtComments" runat="server"></textarea>
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
I found what I wanted, so to all those who will see this:
I just needed to change the type of the button from submit to command, that means adding the attribute UseSubmitBehavior to false.
Thanks for the Answers, this site is great!
The OnClick events run at the server, but the required attrib is preventing the code from being submitted. If you want to run client side (ie javascript) code, then you will need to use the OnClientClick event instead.

Resources