<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged"
Width="90px" AutoPostBack="true" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="Textboxtotalamount"
SetFocusOnError="True" ValidationGroup="val">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="Textboxtotalamount"
ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
SetFocusOnError="True">F</asp:RegularExpressionValidator>
in this above code can validate correctly.......but in text box i call "OnTextChanged"
event ....suppose if i type(characters)in textbox errormesge shown at the same time ontextchanged event also called hen error occured ....i ve to stop OnTextChanged event when regularexpression raised......
pls help
I updated your code. see if this helps you.
<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged" ValidationGroup="val"
Width="90px" AutoPostBack="true" runat="server" CausesValidation="true">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ValidationGroup="val"
ControlToValidate="Textboxtotalamount"
SetFocusOnError="True" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="Textboxtotalamount" ValidationGroup="val"
ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
SetFocusOnError="True">F</asp:RegularExpressionValidator>
I'm not sure I understood your question correctly: You want to execute the `AmountChanged´ code only if the validator did not report any errors, right? In that case, you should call
if (!this.IsValid)
return;
at the start of AmountChanged. (You might need to call this.Validate(); first, if OnTextChanged does not initiate validation by itself.)
Details can be found here: http://msdn.microsoft.com/en-us/library/dh9ad08f(VS.100).aspx
Clarification: At the moment, your code looks like this:
void AmountChanged(...) {
...
}
You need to change it to this:
void AmountChanged(...) {
if (!this.IsValid)
return;
...
}
so that the code is not executed when some validator detects an error. In addition, you must add CausesValidation="true" to your TextBox as Saar's example shows.
Related
I used asp required field in my code but data in text box are sending even they are empty when clicking on the button and after they are sending, message of required field appears!!
Please help! Thanks in advance...
The source code looks like this
<asp:Label ID="LabelUserName" runat="server" AssociatedControlID="UserName" meta:resourcekey="UserName">Nom :</asp:Label>
<asp:TextBox ID="UserName" runat="server" ValidationGroup="val" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Entrez your name " ControlToValidate="UserName" ForeColor="Red" ValidationGroup="val">
</asp:RequiredFieldValidator>
Here is the code of the button :
<asp:Button runat="server" Text="Envoyer" ID="Button1" Width="78px" ValidationGroup="val" />
Can't say for sure without seeing the code inside the event handle for your button click, but my guess is you are missing a check to be sure the page is valid. For example:
If (Page.IsValid) Then
' do your thing
Else
'do nothing
End If
I think the required will help you to solve this issue of field validator
eg:
<asp:TextBox ID="UserName" runat="server" required="required"></asp:TextBox>
I am trying to create a customvalidator to check if an email is already in the database at the point of an user registration. I have seen many similar questions dealing with customvalidators but no answer has brought any avail.
I have simplified the OnServerValidate method to always return false to get ANY sort of response but I am not getting any response. I believe that the method is never being fired. I have come to some conclusion there is some other issue that is effecting the triggering of the event.
Here is the ASP which is inside a contentplaceholder within the master page. There is also a scriptmanager in the master file.
<%--email and confirm email--%>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkEmail" runat="server" TargetControlID="Email" WatermarkText="Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="UserInformation" CausesValidation="True"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red" ControlToValidate="Email" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
<asp:CustomValidator ID="CheckEmail" ForeColor="Red" ErrorMessage="This email already is registered." ValidateEmptyString="true" display="Dynamic" ControlToValidate="Email" runat="server" ValidationGroup="UserInformation" OnServerValidate="checkEmailValidator_ServerValidate"></asp:CustomValidator>
</td>
</tr>
<tr>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkConfirmEmail" runat="server" TargetControlID="ConfirmEmail" WatermarkText="Confirm Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="ConfirmEmail" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ConfirmEmail" ErrorMessage="Confirm E-mail is required." ToolTip="Confirm E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="red" ControlToValidate="ConfirmEmail" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
</td>
</tr>
And then the server-side script. This has a breakpoint at every line and NONE of them are ever getting hit. So I am assuming that the method is never getting triggered.....
protected void checkEmailValidator_ServerValidate(object sender, ServerValidateEventArgs args)
{
args.IsValid = false;
}
And here is the button to move to the next part of the form. It merely just hides one div and shows another to continue the registration process.
<asp:ImageButton ID="NextButton" runat="server" ImageUrl="images/registration/arrowright.png"
ClientIDMode="Static" AutoPostBack="False" OnClientClick="return false;"
Height="50px" onmouseover="this.src='images/registration/arrowrightgreen.png'"
onmouseout="this.src='images/registration/arrowright.png'" ValidationGroup="UserInformation" UseSubmitBehavior="False"/>
This button is used to move to a second part of the registration process, which I use jQuery for. This is why the OnClientClick="return false;" line is there so the jQuery will run correctly. Is there anywhere around this?
$("[id$=NextButton]").click(function (e) {
//alert(Page_ClientValidate("UserInformation"));
if (Page_ClientValidate("UserInformation")) {
$("[id$=pick_user_type]").css('position', 'absolute');
$("[id$=registration_div]").css('position', 'absolute');
$("[id$=registration_div]").hide('slide', { direction: 'left' }, 1000);
$("[id$=pick_user_type]").show('slide', { direction: 'right' }, 1000);
}
});
When you define the validator group of a validator than you to also define the validation group of Controls which the validator is validating.
So define your textbox like this.
<asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"
ValidationGroup="UserInformation"
></asp:TextBox>
If you didnt define the validation group of your button than define there validation group too/
Edit
OnClientClick="return false;" remove this line to make your button postback. If you dont remoove than your form never submited means it will never send request on server.
So I have a textbox with 6 validators. 2 of each kind because I have two validation groups depending on what button the user clicks. My problem is everytime the textbox loses focus two error messages get displayed, and this looks wierd. I've tried setting the CausesValidation property of the textbox to "false" but it's not working.
Textbox & Validators -
<asp:TextBox ID="collectionDtl_Qty" runat="server" CssClass="formText"
AutoPostBack="false" CausesValidation="false" Text="0">
</asp:TextBox>
<asp:FilteredTextBoxExtender ID="collectionDtl_Qty_Filtered" runat="server"
FilterMode="ValidChars" TargetControlID="collectionDtl_Qty" ValidChars="1234567890,">
</asp:FilteredTextBoxExtender>
<asp:CustomValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_CValidator"
runat="server" ClientValidationFunction="coll_QtyValidator"
Text="Quantity cannot be greater than requested quantity." ForeColor="Red"
ValidationGroup="formValidation" Display="Dynamic">
</asp:CustomValidator>
<asp:CustomValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_CValidator2"
runat="server" ClientValidationFunction="coll_QtyValidator"
Text="Quantity cannot be greater than requested quantity." ForeColor="Red"
ValidationGroup="detailValidation" Display="Dynamic">
</asp:CustomValidator>
<asp:CompareValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_Comparer"
runat="server" Text="Quantity must be greater than 0." ForeColor="Red"
ValidationGroup="formValidation" Display="Dynamic"
ValueToCompare="0" Operator="GreaterThan" Type="Integer">
</asp:CompareValidator>
<asp:CompareValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_Comparer2"
runat="server" Text="Quantity must be greater than 0." ForeColor="Red"
ValidationGroup="detailValidation" Display="Dynamic"
ValueToCompare="0" Operator="GreaterThan" Type="Integer">
</asp:CompareValidator>
Any help would be appreciated.
Hi according to your question.what i understand you have to disabled validator or use validation based on Button click.
For Button click use ValidationGroup
ValidationGroup="save"
If you want to disabled validators on condition .Try this one
ValidatorEnable(document.getElementById('<%= rqrgvddlCategory.ClientID %>'), false);
Hope it will helps you.
I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.
In a multigrid I am validating two controls like date and amount. It is validating correctly when I press the tabevent. When I press the save button it is not validating. I am using two validations groups and two validation summary. Then in save button Ialso tried onclientclick() function with javascript it is working fine but if I give the correct value in date and amount records, it is not saving. how to over come this.
Date
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="txtDate" Text='<%# Bind("AD_REF_DATE") %>' runat="server" CausesValidation="true"
ValidationGroup="group" Width="80px" AutoPostBack="true" OnTextChanged="txtDate_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CompareValidator ID="dateValidater" runat="server" ControlToValidate="txtDate"
Operator="DataTypeCheck" Type="Date" ValidationGroup="group" EnableClientScript="true"
ErrorMessage="Please enter a valid date (mm/dd/yyyy)." SetFocusOnError="true" Display="None">*</asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
Amount
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="txtAmount" MaxLength="17" Text='<%# Bind("AD_AMOUNT") %>' CausesValidation="true"
ValidationGroup="req" runat="server" AutoPostBack="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RegularExpressionValidator ID="regVal1" runat="server" ControlToValidate="txtAmount"
ErrorMessage="Format(13int,5deci)" ValidationExpression="^[1-9]\d{0,12}(\.\d{1,2})?%?$"
ValidationGroup="req" Display="None" EnableClientScript="true" SetFocusOnError="true">
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
validations summary:
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ValidationGroup="req"
HeaderText="Amount:Invalid Format" DisplayMode="BulletList" ShowMessageBox="true"
ShowSummary="false" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="group"
HeaderText="Date:Invalid Format" DisplayMode="BulletList" ShowMessageBox="true"
ShowSummary="false" />
button save:
<asp:Button ID="ButtonSave" runat="server" CssClass="button" CausesValidation="true" Text="<%$Resources:TJFAS501, ButtonSave %>"
OnClick="ButtonSave_Click" TabIndex="6" />
How to validate this in the button save also two popup box should be shown?
You might want to try firing the validate function for both groups manually via the button's OnClientClick since you have two validation groups you need to validate against. Currently your validation is not firing because you do not have any ValidationGroup assigned to your button so it is just looking for validators with no ValidationGroup defined (yours groups are: group and req).
You can call Page_ClientValidate() via javascript to fire off the validation checks manually (be sure to set CauseValidation on your button to false) and it has an optional parameter that takes the validation group.
<asp:Button ID="yourButton" runat="server" OnClick="ButtonSave_Click"
CausesValidation="false" TabIndex="6"
OnClientClick="return (Page_ClientValidate('group') && Page_ClientValidate('req'));" />
You can read more about Page_ClientValidate on MSDN.
It would be easier to just have one validation group for each action (eg. your button) but I assume you need two groups for some reason.
Try this..
<script type="text/javascript">
function Validate() {
var isValid = false;
isValid = Page_ClientValidate('Group1');
if (isValid) {
isValid = Page_ClientValidate('Group2');
}
return isValid;
}
</script>