RegularExpressionValidator for textbox inside GridView not working - asp.net

I have GridView:
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It work fine, but when I add RegularrExpressionValidator for textbox with id="tbDate" mygrid does not work
This regular expression must accept datetime in format "dd.MM.yyyy"
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data"
ValidationExpression=
"(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="tbDate">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I think you typed "tbData" instead of "tbDate" in the ControlToValidate property
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data" ValidationExpression= "(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>

I changed ClientIDMode to Predictable for TextBox, RegularExpressionValidator and RequiredFieldValidator and began to work

change ControlToValidate="tbData" to ControlToValidate="tbDate" for <asp:RegularExpressionValidator

Have you tried placing the <asp:RegularExpressionValidator ...> inside the
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate>
also you need to indicate some text to display if it fails the regex validation
actually what Hallie said is more than likely the answer ha

Related

asp.net Gridview checkbox is clickable without edit

I have a checkbox on my griview and its able to be clicked before the edit button is clicked, this doesn't make sense to me and its not something I have came across before. In theory the checkbox should be greyed out until a user clicks the edit button.
Nothing can be updated but it just doesn't make any sense as to why this functionality would be available. I have set up Gridviews before using checkboxes and never came across this. Below is my code:
<asp:GridView ID="gvLeagues" runat="server"
AutoGenerateColumns="False"
onpageindexchanging="gvLeagues_PageIndexChanging"
onrowcancelingedit="gvLeagues_RowCancelingEdit"
onrowdatabound="gvLeagues_RowDataBound"
onrowediting="gvLeagues_RowEditing"
onrowupdating="gvLeagues_RowUpdating"
onsorting="gvLeagues_Sorting" EnableModelValidation="True"
CssClass="footable"
EditRowStyle-CssClass="table table-bordered" >
No Data Found.
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("Name")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created Date" SortExpression="CreatedDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="true" Text='<%# Bind("CreatedDate")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label2" runat="server" Text='<%# Bind("CreatedDate")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day" SortExpression="Day">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Day")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label3" runat="server" Text='<%# Bind("Day")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Season" SortExpression="Season">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Season")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label4" runat="server" Text='<%# Bind("Season")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When using the ItemTemplate, you are the one specifying what and how you want to see it. Just set that checkbox as disabled.
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' enabled="False"/></ItemTemplate>
</asp:TemplateField>

RequiredFieldValidator only works for one validation

I have a web application written in VS2010.
I have a GridView in which I want to add 2 different validations to a field in edit mode. One validation is that it is a required field. The other validation is a max. length of 80 characters.
Below is my HTML:
<asp:GridView ID="UserInfoGridView" runat="server" AutoGenerateColumns="False"
Caption="User Information" CaptionAlign="Top" CssClass="grid" HorizontalAlign="Left"
PageSize="1" Width="400px" ShowHeaderWhenEmpty="true" Height="60px"
OnRowCancelingEdit="UserInfoGridView_RowCancelingEdit"
OnRowEditing="UserInfoGridView_RowEditing"
OnRowUpdating="UserInfoGridView_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="uigvTxtBoxName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldEditName" ControlToValidate="uigvTxtBoxName" runat="server"
ErrorMessage="Required field." ValidationGroup="EditUserNameValidation" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="MaxValEditName" ControlToValidate="uigvTxtBoxName" runat="server"
ErrorMessage="Maximum length is 80." ValidationGroup="EditUserNameValidation" Display="Dynamic"
CssClass="message-error" ValidationExpression="^.{1,80}$">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="uigvLblName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" ItemStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="uigvTxtBoxEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
ErrorMessage="Required field." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="MaxValEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="uigvLblEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Button ID="uigvUpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" CssClass="gridActionbutton"></asp:Button>
<asp:Button ID="uigvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="gridActionbutton"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="uigvEditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" CssClass="gridActionbutton">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Wrap="True" />
</asp:GridView>
When in edit mode if the field is left blank, both of the error messages are displayed. If the user enters more than 80 characters, neither message is displayed.
I did this in an application using VS 2012. Can this be done in VS2010? If so, where am I going wrong?
Thanks.
use regularexpression validator or range validator instead of requiredfield validator for validating max length.
So you need to use one required field and the other is regular expression validator.

How to validate a textbox value based on another textbox value inside gridview in jquery

I have two textbox inside gridview. If 1st text contains any data then 2nd textbox should not be empty. If 1st textbox is null then no validation for 2nd textbox. Only the alert will come after filling the 1st textbox 2nd textbox shouldn't empty. How to do that?
Any idea? Please help me out.
<asp:GridView runat="server" Width="980px" ID="grdResUpdate" AutoGenerateColumns="false"
CssClass="TopMargin10 borderClass gridwrap" OnRowDataBound="grdResUpdate_RowDataBound" ShowHeader="true">
<Columns>
<asp:TemplateField HeaderText="SO #" ItemStyle-Width="70px">
<ItemTemplate>
<asp:Label runat="server" Width="70px" ID="lblSOName" CssClass="gridwrap" Text='<%# Eval("SOName")%>' />
<asp:HiddenField runat="server" ID="hdnFldSOId" Value='<%#Eval("SOId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Type" ItemStyle-Width="300px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:HiddenField runat="server" ID="testTypeIdHdnFld" Value='<%#Eval("TestTypeId") %>' />
<asp:Label runat="server" Width="150px" ID="lblTestTypeName" CssClass="gridwrap" Text='<%# Eval("TestTypeName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Count" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTestCaseCount" runat="server" HeaderText="Test Case Count" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Environment" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtEnvironment" runat="server" HeaderText="Environment" Width="80px" onkeydown="return isDigit(event)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPassed" runat="server" HeaderText="Passed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Failed" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtFailed" runat="server" HeaderText="Failed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Defects" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtDefects" runat="server" HeaderText="No Of Defects" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hours" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtHours" runat="server" HeaderText="Hours" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTCComp" runat="server" HeaderText="Test Case Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPercComp" runat="server" HeaderText="Percentage Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerTop" />
<AlternatingRowStyle CssClass="rowbgblue" />
<RowStyle CssClass="rowbginwhite" />
</asp:GridView>
I want when the user fills the txtTestCaseCount then he must have to fill txtHours
can check with onfoucsOut event
$("textbox2id").focusOut(function(){
if(!($("textbox2id").val() == ""))
{
alert("please enter a value as required");
$(this).focus();
}
})
First access gridview text boxes and then place checking code inside change event of txtTestCaseCount textbox
var textBoxOne = '#<%=grdResUpdate.ClientID%> input[id*="txtTestCaseCount"]';
var textBoxTwo = '#<%=grdResUpdate.ClientID%> input[id*="txtHours"]';
$(textBoxOne).on('change', function () {
if($(textBoxTwo).val()==""){
alert("Please fill hours textbox");
}
});
this checks if txtTestCaseCount value is filled then if textbox txtHours is not filled then alert message is shown.
Every textbox in gridview has an id generated at runtime. Use that id as a selector, like
$("#textboxid").val()
.This will give u the content of the textbox. Use it to validate the other textbox.
If suppose the id generated of txtTestCaseCount is like grdResUpdate_txtTestCaseCount then
if($("#grdResUpdate_txtTestCaseCount").val().length < 0){//Code}

Telerik Radgrid required field issue

I have a radgrid displayed on a page and am wanting to make a particular field required when in insert and edit mode but don't necessarily know how to go about this.
Using regular validators
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*" ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
Like this:
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="ContactName" UniqueName="TemplateColumn">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
http://www.telerik.com/help/aspnet-ajax/grid-validation.html
For common validation like required field, I found RadGrid has it built-in support
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/validation
<telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
<ColumnValidationSettings EnableRequiredFieldValidation="true" EnableModelErrorMessageValidation="true">
<RequiredFieldValidator ForeColor="Red" ErrorMessage="This field is required"></RequiredFieldValidator>
<ModelErrorMessage BackColor="Red" />
</ColumnValidationSettings>
where ModelErrorMessage control validation is available only in .NET 4.5 when ModelBinding is used.

validation of edit row and insert row in GridView

Simple question? I have a gridview with an insert row in the footer and i have validation controls for that row and the edittemplate how do get it to only validate for that row because when i do an edit it validates the insert row aswell. I just wnat to validate the insert row or edit row.
<asp:GridView ID="PageSettings" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
DataKeyNames="Id"
DataSourceID="ObjectDataSourcePages"
OnLoad="PageSettings_Load"
OnRowDataBound="PageSettings_DataBound"
OnRowCommand="PageSettings_RowCommand"
OnRowCreated="PageSettings_RowCreated"
OnRowEditing="PageSettings_RowEditing"
OnRowCancelingEdit="PageSettings_RowCancelingEdit"
OnRowUpdating="PageSettings_RowUpdating"
OnPageIndexChanging="PageSettings_PageIndexChanging"
OnSorting="PageSettings_Sorting"
OnSorted="PageSetting_Sorted"
PageSize="2"
ShowFooter="True"
ShowHeaderWhenEmpty="True">
<Columns>
<asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireName" runat="server"
ControlToValidate="Name"
CssClass="gridview-error"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertName" runat="server"
ControlToValidate="InsertName"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
<ItemTemplate>
<%# Eval("Path") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequirePath" runat="server"
ControlToValidate="Path"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertPath" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertPath" runat="server"
ControlToValidate="InsertPath"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
<ItemTemplate>
<%# Eval("RouteValue") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRouteValue" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
<ItemTemplate>
<%# Eval("RegExp") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRegExp" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use a
Validation Group
Define a separate group for each operation
SaveRecords
&
EditRecords
. You have to specify each validation group on the Save/Edit buttons & all corresponding validation controls to fire them for that event.
MSDN
After a long Time
but you must add in footer requiredfieldvalidator a validationgroup with a unique name and add this validationgroup to add button

Resources