ASP.NET:Required Field Validator not working - asp.net

I've got a weird problem that i don't understand why it happened.
I made a content page so a user can send me a message to my email and made a few TextBox's that are linked to some validators.
when i first made the page i hed 1 Regular Expression validator and every thing worked out fine like i planed it, But then i decided to delete the Regular Expression validator and now i got a problem with the Required Field validator.
when i press submit the page do a post back like there isn't any validators on the page.
I'm pretty sure I'm missing something (I'm pretty new at asp.net)
here is the markup:
<!-- Regular Expression Validtor for the Name Text Box
<asp:RegularExpressionValidator ID="NameExpressionValidator1" runat="server" ControlToValidate="nameTextBox"
ValidationExpression="[אבגדהוזחטיכלמנסעפצקרשתץףןם\s\.]*" Display="Dynamic"> </asp:RegularExpressionValidator> -->
<!--The Name Text Box -->
<asp:TextBox ID="nameTextBox" class="TextBoxes" runat="server"
AutoPostBack="True" ValidationGroup="g" ></asp:TextBox> <span class="infoText">:שם</span>
<!-- Required Field Validator for the Name Text Box -->
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="nameTextBox"
Display="Dynamic" ValidationGroup="g" ErrorMessage="please enter your name"></asp:RequiredFieldValidator>
<br />
<br />
<!-- Phone Text Box -->
<asp:TextBox ID="phoneTestBox" class="TextBoxes" runat="server"
AutoPostBack="True" ValidationGroup="g" ></asp:TextBox> <span class="infoText">:טלפון</span>
<!-- Requierd Field Validator Phone Text Box -->
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="phoneTestBox"
Display="Dynamic" ValidationGroup="g" ErrorMessage="please enter your phone"></asp:RequiredFieldValidator>
<br />
<br />
<asp:TextBox ID="messagTextBox" runat="server" class="TextBoxes" TextMode="MultiLine"
Text="asdasdadasdasdasd"></asp:TextBox> <span class="infoText">:הודעה</span>
<asp:Button ID="button" runat="server" Text="send message" ValidationGroup="g" />

You should not use HTML comments to comment out server side controls but <%-- --%>:
<%--
<asp:RegularExpressionValidator ID="NameExpressionValidator1" runat="server" ControlToValidate="nameTextBox"
ValidationExpression="[אבגדהוזחטיכלמנסעפצקרשתץףןם\s\.]*" Display="Dynamic"> </asp:RegularExpressionValidator>
--%>
http://msdn.microsoft.com/en-us/library/4acf8afk.aspx

Is there any specific use of AutoPostBack="True" in textbox? if not then just remove it from all textboxes, also you need to give ValidationGroup="g" for only validator controls, there is no need to give it in textbox controls.

first of all, ensure the validation group of the validation summary matches?
If that's correct try using validation method:
protected void Page_Load(object sender, EventArgs e)
{
Validate();
}

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.

ASP.NET Required Field Validator & Custom Validator not working

This is my aspx code
<p class="input-block last">
<asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10" ValidationGroup="EmployerRegister" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="uxEmployerName" ErrorMessage="You must enter Company Name." EnableClientScript="false" Display="none" ValidationGroup="EmployerRegister" />
</p>
<p class="input-block last">
<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="false" 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" />
</p>
<p class="input-block last">
<asp:LinkButton runat="server" ID="uxRegisterButton" Text="Submit" CausesValidation="true" ValidationGroup="EmployerRegister" CssClass="button" TabIndex="18" />
<a class="button" href="#" tabindex="17">Edit package</a>
</p>
the only validator works here is RegularExpressionValidator
if I input wrong email format then it shows the error message. but not if i leave the email field.
What am i doing wrong. any solution
i have also added a custom validator with the email field
<asp:CustomValidator ID="uxEmailValUnique" runat="server" ErrorMessage="It appears that you already have an account with us. <br/><a href='forgotten-password.aspx'>Request password</a>"
ControlToValidate="uxEmail" EnableViewState="False" ValidationGroup="EmployerRegister"
Display="None" />
the backend server code for this 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
where in LocalHelper file
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
this does not work either
what am i doing wrong.
I assume that you have a ValidationSummary control similar to this:
<asp:ValidationSummary runat="server" ForeColor="Red" ValidationGroup="EmployerRegister" />
If any of your validators has client-side validation enabled (EnableClientScript="true", the default value), it will prevent the postback when the client validation fails, so that server-side validation will not be performed. In your current code, the RegularExpressionValidator has client-side validation enabled (not explicitly disabled, in fact); if it fails, the server-side validation for the required fields is not performed and the other error messages are not displayed.
You could disable the client-side validation for all your validators. The message would appear for all of them (if appropriate) every time you submit the form.
<asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="false" ... />
<asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="false" ... />
<asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="false" ... />
<asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
Another reasonable approach could be to enable the client-side validator for the most basic requirements, and disable it for the CustomValidator. The more advanced validation does not make much sense if the e-mail is empty or not valid in the first place.
<asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="true" ... />
<asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="true" ... />
<asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="true" ... />
<asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
N.B. In your question, you say that the validation message is not displayed when the e-mail field is empty. I haven't seen that behavior with your original code in my tests.

Does the location of requiredField (inside a ListView) has incidence on the way ValidationSummary display errors?

I have 2 RequiredFieldValidator controls inside a ListView. When validation occurs, the ValidationSummary control located at the top of the page (outside of the ListView) doesn't display any error.
EDIT
Here's the code.
<asp:TextBox ID="_txtClientName" width="75px" runat="server" TabIndex="4" /><br />
<asp:Button ID="_btnSearch" CssClass="grey_button" width="50px" runat="server" Text="Search"
TabIndex="5" CommandArgument="search" ValidationGroup = "SearchClientName" />
<asp:RequiredFieldValidator ID="reqSearch" runat="server" Text = "*" ErrorMessage="Please enter criteria to search" ControlToValidate = "_txtClientName" ValidationGroup = "SearchClientName" />
The summaryValidation control is located outside the ListView, at the very top of the page.
<asp:ValidationSummary ID="ValidationSummary1" ShowSummary = "true" runat="server" />
Thanks for helping.
Add to your two RequiredFieldValidators : Display="None"
And to your summary : ShowSummary="true"

Disable an asp:RequiredFieldValidator if a checkbox is checked

I have 2 textboxes with required field validators, a button and a checkbox. When the check box is checked I want to disable the required field validator on the second textbox and allow the button to be clicked when the first textbox is completed.
This is generally what my aspx page looks like, although this is just a made up example:
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="Name" Display="Dynamic" ErrorMessage="Name Required" runat="server" />
Name: <asp:Textbox ID="Name" runat="server"/>
<asp:RequiredFieldValidator ID="rfvID" ControlToValidate="Name" Display="Dynamic" ErrorMessage="ID Required" runat="server" />
ID: <asp:Textbox ID="ID" runat="server"/>
<asp:Button ID="btn" Text="Query" runat="server"/>
<asp:CheckBox ID="chxNoID" Text="I don't have an ID yet" runat="server" />
use javascript. Get the checkbox change event & put the code below in function
var ctrl1 = document.getElementById('<%=rfvID.ClientID%>');
ValidatorEnable(ctrl1, false);

ASP.NET Validation styling

I'm using ASP.NET WebForms and using some validation like the following:
<asp:TextBox runat="server" ID="txtUserName" CssClass="Email" /><br />
<asp:RequiredFieldValidator runat="server" ID="rfvUserName" Display="Dynamic" ControlToValidate="txtUserName" ErrorMessage="Please enter your email address" CssClass="Error" />
<asp:RegularExpressionValidator Enabled="false" runat="server" ID="revUserName" Display="Dynamic" ControlToValidate="txtUserName" ErrorMessage="Please enter a valid email address" ValidationExpression="<%$ Resources:Global, RegExEmail %>" />
What I want to do is instead of just showing a message below in the form of a span I would like to alter the textbox itself, such as add a red border around it and change its background to have an error icon. How do I do this? Thanks.
what about ValidatorCallout? It looks nice and let you add custom CSS class to validated field when it's content is invalid.Also it's a part of popular Ajax Toolkit by Microsoft.

Resources