Validation Not Firing On TextBox - asp.net

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.

Related

RegularExpressionValidator in Asp.net

I have search textbox,which has default value Enter Month to View Profit. When I click search button without entering any data, the default value of textbox is posted to server for search. I want that RegularExpressionValidator do not validate default value of textbox.
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!"
>
</asp:RegularExpressionValidator>
The default for all validators except the RequiredFieldValidator control if you post with empty field the validator will not trigger
you must use required field validator with other validators to prevent the postback to happen
from MSDN
Special-Case Validation Results for ASP.NET Server Controls
EDIT
Also if you add your controls as in your question it should work but if there is other controls like for example other button you should set the validationGroup Property to the group that you want to work together
ValidationGroup="vGrp"
and your code will be like this
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server" ValidationGroup="vGrp"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" ValidationGroup="vGrp" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
ValidationGroup="vGrp">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!" ValidationGroup="vGrp"></asp:RegularExpressionValidator>
from MSDN about validationGroup

Required Field Validation not working in asp.net

I have required field validation control for a radiobutton list. So if no values are selected then it gives me a error which is fine. But when i redo select something and click the button then it does't not fires the server event of the button. Once i have the validation erro then whatever i do it disable the server side event.
any ideas why is it happening my code
<div id="studysub_popul" runat="server" visible="false">
<asp:Label ID="lbl_rdb_study_popul" runat="server"
CssClass="questions"
Text="2.Select your study subjects">
</asp:Label>
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged">
<asp:ListItem>Individuals</asp:ListItem>
<asp:ListItem>Population</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="rdb_study_popul"
Display="Dynamic"
ErrorMessage="Study Subject is required"
ValidationGroup="StudySubject">
</asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn_s_section" runat="server"
OnClick="btn_studysubject_section_Click"
Text="Next" ValidationGroup="StudySubject"
Visible="false" />
</td>
You should add a validating group to the RadioButtonList definition too.
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged"
ValidationGroup="StudySubject">

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

Can I have RequiredFieldValidator to check for more than one ControlToValidate?

I am having 3 TextBoxes in a row and then a Next button.
On clicking Next button, the control would go to the next page only if ALL of the 3 TextBoxes contain some user-entered text.
Is there any option by which I can check whether all of the TextBoxes contain some text by using RequiredFieldValidator (rather than going for different RequiredFieldValidator for each ControlToValidate)?
I am currently having following code:
<tr>
<td class="style1">
<asp:Label ID="lblDOB" runat="server" Font-Bold="True" Font-Italic="False" Font-Size="Medium"
ForeColor="Black" Text="Date of Birth"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtA" runat="server" Width="45px"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server" Width="45px"></asp:TextBox>
<asp:TextBox ID="txtC" runat="server" Width="45px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="txtA"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
ControlToValidate="txtB"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server"
ControlToValidate="txtC"
ErrorMessage="Please enter Text">
</asp:RequiredFieldValidator>
</td>
</tr>
So, instead of having 3 different RequiredFieldValidators, I want to use only a single RequiredFieldValidator which checks if all the 3 TextBoxes contain some text. How can I achieve this?
Your best bet is to use CustomValidator and combine with either client side javascript or server side c#/vb.net code.
You can't validate more than one control using RequiredFieldValidator.
you can't use the RequiredFieldValidator with more than one control ,
but i prefer to use the HTML5 required attribute instead of ASP.NET Validation.
<asp:TextBox ID="txt1" required="required" />

ASP.NET Validator doesn't fire

I've a problem with a RequiredFieldValidator on a page with two Panels, which represent two different views, i.e. only one panel is visible at one time, but they share same submit button.
By default the "BatchReturnPanel" is visible and "SingleReturnPanel" is hidden, and in this case all validators on page work just fine. But if I hide the "batch.." panel and show "single.." panel (ChangeViewButton click event) then the requieredFieldValidator "DropDownListFieldValidator" doesn't fire up, and page is always considered validated. Why does this happen and how can this problem be solved? Thanks for help/
Below is extract from the ASPX page:
<div>
<asp:DropDownList ID="MDDropDownList" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator id="DropDownListFieldValidator" runat="server" ControlToValidate="MDDropDownList"
InitialValue="-" ErrorMessage="Please select Master Distributor" EnableClientScript="true" CssClass="error"
Display="Static" ValidationGroup="Voucher" />
<asp:Panel runat="server" ID="BatchReturnPanel">
<p>
<asp:TextBox ID="FirstVoucherTextBox" runat="server" CssClass="Scanner RRS"></asp:TextBox>
<asp:RequiredFieldValidator ID="FirstVoucherFieldValidator" runat="server" ControlToValidate="FirstVoucherTextBox" ValidationGroup="Voucher"
ErrorMessage="Valid Voucher ID for the first voucher in batch is required<br>" Display="Dynamic" CssClass="error" EnableClientScript="false"></asp:RequiredFieldValidator>
</p>
</asp:Panel>
<asp:Panel runat="server" ID="SingleReturnPanel" Visible="false">
<p>
<asp:TextBox ID="SingleVoucherTextBox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="SingleVoucherTextBox2" runat="server"></asp:TextBox><br />
<asp:TextBox ID="SingleVoucherTextBox3" runat="server"></asp:TextBox><br />
</p>
</asp:Panel>
<asp:Button ID="VoucherSubmitButton" runat="server" Text="Enter"
OnClick="VoucherSubmitButton_Click" UseSubmitBehavior="false" ValidationGroup="Voucher"/>
</div>
<asp:Panel CssClass="grid_4 box" runat="server" ID="ChangeViewPanel">
<asp:LinkButton runat="server" ID="ChangeViewButton"
onclick="ChangeViewButton_Click">Single item return mode</asp:LinkButton>
</asp:Panel>
You can use validation group and client side page validation.
function Validation() {
Page_ClientValidate('validationGroupName');
if (!Page_IsValid) {
return false;
}
else {
return true;
}
}
Hope this helps

Resources