RequiredFieldValidator Control displays the Error Text on Page_Load - asp.net

I am having trouble with the <asp:RequiredFieldValidator> in this code.
<asp:Label ID="email_Label" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="email_Text" runat="server" MaxLength="40" Width="250"></asp:TextBox> *
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" Text="Required"></asp:RequiredFieldValidator>
According to the W3schools documentation here I have used the <asp:RequiredFieldValidator> correctly, however instead of displaying the label and text box the page also displays the error message. This happens on page_load so the value has not had a chance to change from the default yet. I want the error text to display after the user clicks the Save button at the bottom of the form I am building.
What is Being Displayed:
Email[TextBox] * Required
What Should Be displayed:
Email[TextBox] *
Am I missing a parent element for the validator or something. According to the example on w3schools site no parent element should be needed. In fact the way they have their example set-up is exactly what I was expecting for this.

Use Validation Group if want to display Error message on Button click. Like this.
<asp:RequiredFieldValidator ID="rqtxtQName" ValidationGroup="save" ControlToValidate="txtQueueName" runat="server" ErrorMessage="Some required field are missing." SetFocusOnError="True"Display="Dynamic"></asp:RequiredFieldValidator>
and use validation group on Button also.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="save" OnClick="btnSubmit_Click"/>
Hope it will helps.

you should use ErrorMessage instead of Text
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" ErrorMessage="Required"></asp:RequiredFieldValidator>

If you want to validation on save button click event then set property ValidationGroup="Group1"
for RequiredFieldValidator and also for save button. So it will check validation when you click save button.
And For display message you can use ErrorMessage property.
Thanks,
Hitesh

Set the RequiredFieldValidator this way. Remove the property: 'TextRequired' & put a * inbetween the opening and closing tag.
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="email_Text">*</asp:RequiredFieldValidator>
Also, Do not set the ErrorMessage property as this will again not fit your requirements.

Related

Hide validations on form post back in aspx page

I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.

RequiredFieldValidator have to click twice

I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.

How to make regex validator in ASP.NET to stop insertion of data?

I have a grid view in ASP.NET, one of the column of the grid is time(hh:mm). I have used validators for the field. They look like this
<FooterTemplate>
<asp:TextBox ID="st_timef" runat="server"/>
<asp:RegularExpressionValidator ID="stRegularExpressionValidator" runat="server" ErrorMessage="HH:MM!"
ValidationExpression="/(?:[01]?\d|2[0-3]):(?:[0-5]\d)$/" ControlToValidate="st_timef" />
<asp:RequiredFieldValidator ID="stValidator" runat="server" ControlToValidate="st_timef" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
The validators work,but I have a problem with the regex validator,
Although it shows a error on wrong input format,but it still allows me to press the button which updates the data,(which is not the case of normal validator).
I need the validator to stop running my update function ones it shows a error in validation.
You must have to set the common validation group name of ValidationGroup property for TextBox, Button and RegularExpression validator.
how to make the error disappear once the correct format is inserted?
Set error description to the Text property of validation control (Set empty string to ErrorMessage property).

Eval property and extra text on a button's text property

I'm using an asp Button like follows:
<asp:Button id="someButton" runat="Server" Text='<%# Eval("Size") %>' />
This is great but before the Eval Property result I wanted to add the Text "Download " with possibly the units after it.
eg. Download 123KB.
Can someone please tell me how to go about this?
In this case it might be easier to set the text property of the button on the server in the page load event.
insted of button use linkButton.
**asp:LinkButton ID="HyperLink2" OnClick="HyperlinkSelectedCustomers_Click" runat="server" CommandArgument='<%#Eval("id") %>' ><%#Eval("selectedCustomers") %>/asp:LinkButton

RequiredFieldValidator doesn't work when submitting form after Posting back by TextChanged event to the Textbox

In my .aspx form I have a text box with Autopostback=True.
I also set a RequiredFieldValidator next to it. But when Posting back by TextChanged event of my text box, my RequiredFieldValidator appears but still I can submit my form with nothing in the text box!
What is wrong, and how can I fix this?
You have to set the textbox's CausesValidation property to true (it's set to false by default for textboxes).
Give both the text box and the submit button the same validation group.
try this code
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ForeColor="Red" ControlToValidate="TextBox2" Display="Dynamic" runat="server" />
<asp:Button ID="btn" Text="button" CausesValidation="true" runat="server"/>
and make sure that ControlToValidate="" has same name as the <asp:texbox/>has
the best way to do is just right below TextBox field type <asp:Required and when the options come just pres TAB Button twice

Resources