I have a question. I have a textarea and 2 textboxes. What I want to do is to check if textarea is filled, other 2 textboxes must not be empty. We can give messagebox like this (please necessary fields must be filled)
This is my code :
<textarea id="txtCustomerRepresentatorOpinion" name="txtCustomerRepresentatorOpinion" runat="server" style="width: 99%"></textarea>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCustomerRepresentatorOpinion"
ValidationGroup="Save" Text="*" ClientValidationFunction="txtArea" />
<telerik:RadTextBox Width="321px" runat="server" ID="txtCustomerRepresentatorOpinionNameSurname" TextMode="SingleLine" />
<asp:RequiredFieldValidator ID="VldAdi" runat="server" ControlToValidate="txtCustomerRepresentatorOpinionNameSurname"
ValidationGroup="Save" Text="*" ErrorMessage="Lütfen Adı Soyadı giriniz."/>
<telerik:RadDatePicker ID="rdpCustomerRepresentatorOpinionDate" ZIndex="9999" Style="vertical-align: middle;" runat="server">
</telerik:RadDatePicker>
<asp:RequiredFieldValidator ID="VldTarih" runat="server" ControlToValidate="rdpCustomerRepresentatorOpinionDate"
ValidationGroup="Save" Text="*" ErrorMessage="Lütfen Tarihi giriniz.">
</asp:RequiredFieldValidator>
Which side should I use (client side or server side)? How i do this job? Right now, I can show messages but textarea is not working.
Change your textarea into an asp.net TextBox Control with TextMode="MultiLine". Then a RequiredFieldValidator will work.
<asp:TextBox ID="txtCustomerRepresentatorOpinion" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required field" ControlToValidate="txtCustomerRepresentatorOpinion"></asp:RequiredFieldValidator>
Related
I have spent hours on this and read countless threads but not sure what i may be missing to get this to work the way i would like.
I would like to select a file to upload and when the user clicks the button to save the record if there are any validation errors to retain the value of file that was selected to upload.
Heres my markup
<asp:MultiView runat="server" ID="RegisterMultiView">
<asp:View ID="DefaultView" runat="server">
<asp:UpdatePanel ID="upCountryBrands" runat="server">
<ContentTemplate>
Select parent
<asp:RequiredFieldValidator
SetFocusOnError="true"
runat="server" ID="RequiredFieldValidator3"
ControlToValidate="ddlParent" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlParent" runat="server"
OnSelectedIndexChanged="ddlParent_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
Select Child
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator2"
ControlToValidate="ddlChild" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlChild" runat="server"
OnSelectedIndexChanged="ddlChild_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Name
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator1"
ControlToValidate="txtName" ValidationGroup="MainForm" ErrorMessage="Please enter your name" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:TextBox ID="txtName" runat="server" MaxLength="100" />
Enter description of your issue
<asp:TextBox ID="txtIssue" runat="server" TextMode="MultiLine" /> <%--This is a Tiny MCE control--%>
Image 1
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="rfv1" ControlToValidate="FileUpload1" ValidationGroup="MainForm"
ErrorMessage="Please select an image." Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFileTypes" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload1" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" accept="image/*" ViewStateMode="Enabled" EnableViewState="true" />
<asp:Label ID="lblImageOneStatus" runat="server"></asp:Label>
Image 2
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator9"
ControlToValidate="FileUpload2" ValidationGroup="MainForm" ErrorMessage="Images only" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload2" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" class="form-control-file" accept="image/*" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="MainForm" OnClick="btnSave_Click" OnClientClick="txtIssue.triggerSave(false,true);" />
</asp:View>
<asp:View ID="SuccessView" runat="server">
Thanks
</asp:View>
</asp:MultiView>
Couple of notes:
I have an updatePanel surrounding the dropdown so it looks like theres no postback.
I have tried surrounding the FileUpload controls with an UpdatePanel but this made no difference.
I have a TonyMCE control, i dont think this would add a spanner into the works but i have left out the associated Javascript so i can provide if required.
I have tried adding triggers but again this made no difference so i can only assume i am doing something wrong.
How could i ensure when a file is selected using the FileUpload controls and then press Save it retains the value of the file?
Required Field Validator not working properly in gridview
I work in ASP.NET c# and MySQL database.
I need to make the required field of this DropDownList on FooterTemplate, but Required Field Validator not working.
Why ?
My code:
<FooterTemplate>
<asp:ImageButton ID="imgbtnInsert" runat="server" CommandName="Insert" ImageUrl="/images/icon_2.gif"
ToolTip="Add" OnClientClick="return confirm('Confirm?');" />
</FooterTemplate>
<FooterTemplate>
<asp:DropDownList ID="txtIDDTES" runat="server" CssClass="ddl_Class_new">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rtxtIDDTES" runat="server" ControlToValidate="txtIDDTES"
ErrorMessage="***" Text="*" Display="Dynamic" ValidationGroup="Validation1"
SetFocusOnError="true" EnableClientScript="true"></asp:RequiredFieldValidator>
</FooterTemplate>
<asp:ValidationSummary ID="Validation1" ValidationGroup="Validation1" runat="server" ShowMessageBox="true" />
Can you please help me figure out the problem?
Thanks in advance.
Try this :
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummaryTop" runat="server" ShowMessageBox="true" ShowSummary="false" />
...
<FooterTemplate>
<asp:DropDownList ID="txtIDDTES" runat="server" CssClass="ddl_Class_new">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rtxtIDDTES" runat="server" ControlToValidate="txtIDDTES"
ErrorMessage="***" Text="*" Display="Dynamic"></asp:RequiredFieldValidator>
</FooterTemplate>
I have a repeater with editable rows on the aspx page. The row has a textbox and a required field validator associated to it. On 'save' button click on that row, the required field valiator fires, It is tied to a validationsummary at the top of the page.
Now I have this panel below the repeater with a text box and a save button. This panel is 'opened' using the jquery show method on the click on a linkbutton at the top of this panel.
The required field validator is not firing for this texbox. I have a different validation summary for this textbox; as I need to trigger the validations on click on its save button only.
So I have the code like this,
<asp:ValidationSummary ID="validationSummary" ValidationGroup="ValidationGroup1" EnableClientScript="true" runat="server" />
<asp:ValidationSummary ID="validationSummary1" ValidationGroup="ValidationGroup2" EnableClientScript="true" runat="server" />
<asp:Repeater ID="Teams" runat="server" OnItemCommand="ItemCommand" OnItemDataBound="ItemDataBound">
<HeaderTemplate>
<table >
<tr>
<th>
<asp:Label ID="lbTeamNameHeader" runat="server" Text="TeamNameHeader"></asp:Label></th>
<th></th> </tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="txtTeamName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txtTeamName")%>' />
<asp:RequiredFieldValidator ID="reqdFieldValTeamName" ErrorMessage="Field cannot be blank" ValidationGroup="ValidationGroup1" runat="server" Display="None" ControlToValidate="txtTeamName"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regularExprTeamName" ErrorMessage="Invalid input for the team name" ValidationGroup="ValidationGroup1" Display="None" ControlToValidate="txtTeamName" runat="server" ValidationExpression="^[a-zA-Z0-9]+$"></asp:RegularExpressionValidator>
</td>
<td>
<asp:Panel ID="panelSave" runat="server" Visible="false">
<asp:LinkButton ID="linkbuttonSave" runat="server" CommandName="Save" ValidationGroup="ValidationGroup1" Text="SAVE" />
<asp:LinkButton ID="linkbuttonCancel" runat="server" CommandName="Cancel" Text="CANCEL" />
</asp:Panel>
</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<asp:LinkButton ID="linkbuttonAddTeam" runat="server" Text="Add New Team" />
<div id="AddPanelDiv" style="display:none;">
<asp:TextBox ID="txtAddTeam" runat="server" />
<asp:RequiredFieldValidator ID="reqdFieldValAddTeam" ErrorMessage="Field cannot be blank" ValidationGroup="ValidationGroup2" runat="server" Display="None" ControlToValidate="txtAddTeam"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regularExprValAddTeam" ErrorMessage="Invalid format" ValidationGroup="ValidationGroup2" Display="None" ControlToValidate="txtAddTeam" runat="server" ValidationExpression="^[a-zA-Z0-9]+$"></asp:RegularExpressionValidator>
<asp:LinkButton ID="linkbuttonCancel" runat="server" Text="CANCEL"></asp:LinkButton>
<asp:LinkButton ID="linkbuttonSaveNewTeam" runat="server" OnClick="linkbuttonSaveNewTeam_OnClick" Text="SAVE" CausesValidation="true" />
</div>
Why on earth the validators for the bottom panel fire? On click of that save button, it straightaway goes to the page method onclick.
If I remove the validation group and click on the save in the repeater, it fires! But thats not what I want..
You are missing ValidationGroup="ValidationGroup2" on your second button it looks like.
<asp:LinkButton ID="linkbuttonSaveNewTeam" runat="server" OnClick="linkbuttonSaveNewTeam_OnClick"
Text="SAVE" CausesValidation="true" ValidationGroup="ValidationGroup2" />
Is it possible to show the error message of a Regular Expression validator in Asp.net into a message box/ dialog box?
I've tried everything i knew but nothing works for me.
Thanks alot
<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>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="None" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<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="None" 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>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
ShowSummary="False" ValidationGroup="Validation" />
</form>
use the same ValidationGroup text on all validation controls and add a ValidationSummary with the ValidationGroup and ShowMessageBox="true"
So with the suggestions i updated my code.
Textbox:
<asp:TextBox ID="txtStudent_Id" runat="server"></asp:TextBox></td>
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtStudent_Id"
ErrorMessage="Id is required"
ID="validator_ID"
ValidationGroup="Validation_ID">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtStudent_Id"
ValidationExpression="\d+"
Display="Static"
EnableClientScript="true"
ErrorMessage="Please enter numbers only" ValidationGroup="Validation_ID"
runat="server"/>
Button:
<asp:Button runat="server" Text="Submit" ID="btnSubmitStudent" ValidationGroup="Validation_ID" CausesValidation="true" OnClick="btnSubmitStudent_Click" />
Code Behind:
addstudent.Student_Id =Convert.ToInt32(txtStudent_Id.Text);
Now that i have also entered the regular expression validator it should work but i still get the same error.
Don't need a solution for your error.need a solution why the validation control not works? and how can solve this problem?then what i do?
Main thing
You have missed ValidationGroup="XXXXX" in validation control and button control, So it's allowed to server side code, so first add that and
try with this asp:RegularExpressionValidator for only allow integer value, So It's not allow if you type any special and string characters
<asp:TextBox ID="txtStudent_Id" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtStudent_Id" ValidationGroup="XXXXX" EnableClientScript="True"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtStudent_Id"
ValidationExpression="\d+"
Display="Static"
EnableClientScript="true"
ErrorMessage="Please enter numbers only" ValidationGroup="XXXXX"
runat="server"/>
<asp:button ValidationGroup="XXXXX"></asp:button>
or
<asp:button ValidationGroup="XXXXX" CasusesValidation="true"></asp:button>
If you try my answer,then don't worry about Input string was not in a correct format. error.
Update:
please try this in your button click event.
Page.Validate();
if (Page.IsValid == false)
{
return;
}
//Call your code here
TextBox
<asp:TextBox ID="txtStudent_Id"
runat="server"
ValidationGroup="VAlidation_Name">
</asp:TextBox>
RequiredFieldValidator
you have not added id to the RequiredFieldValidator and also ValidationGroup
<asp:RequiredFieldValidator runat="server"
Display="Dynamic"
ControlToValidate="txtStudent_Id"
ID="rfvStudent_Id"
ForeColor="Red"
ValidationGroup="VAlidation_Name"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>
Button Submit
<asp:Button ID="btnSubmit" Text="Submit"
runat="server"
ValidationGroup="VAlidation_Name" CausesValidation="true" />
In default CausesValidation="true" but check that too.
Code Part
addstudent.Student_Id =int.TryParse(txtStudent_Id.Text);
Suggestion:
And you can use AjaxToolKit Extender MaskedEdit to make the user enter only integer.
<asp:MaskedEditExtender ID="txtEndDate_MaskedEditExtender" runat="server" ClearTextOnInvalid="True"
ClearMaskOnLostFocus="true" Enabled="True" Mask="9999999"
MaskType="Date" TargetControlID="txtStudent_Id"
ErrorTooltipEnabled="True">
Updated:
void SubmitButton_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
// Do form stuff
}
Check this too EnableClientScript="True" and Enabled="True"
<asp:RequiredFieldValidator runat="server"
Display="Dynamic"
ControlToValidate="txtStudent_Id"
ID="rfvStudent_Id"
EnableClientScript="True"
Enabled="True"
ForeColor="Red"
ValidationGroup="VAlidation_Name"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>