Required+regular validation - asp.net

<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

try this
<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged" Width="90px" AutoPostBack="true"
CausesValidation="true" ValidationGroup="val" 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" ValidationGroup="val"
ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$" SetFocusOnError="True">F</asp:RegularExpressionValidator>
i've added. the CausesValidation="true" and ValidationGroup="val" to the TextBox.
and ValidationGroup="val" to the RegularExpressionValidator
now the OnTextChanged will only fire when all the validators in the group "val" succeed

Related

RadMaskedTextBox ssn format

I have a RadMaskedTextBox for SSN while entering value in it, the first two characters are clearing up itself and getting RegularExpressionValidator message. Can some body help me in this.This problem exits in IE browsers only.
<span id="SSN" runat="server">*</span>Social Security Number</label>
<asp:RegularExpressionValidator ID="TaxId_RegEx"
runat="server" ControlToValidate="TaxId"
Display="None" ValidationExpression="^\d{3}\-\d{2}\-\d{4}$" ErrorMessage="Please enter a valid 9 digit SSN." Enabled="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="TaxId_ReqField"
runat="server" ControlToValidate="TaxId"
Display="None" ErrorMessage="Please enter a value for this required field."></asp:RequiredFieldValidator>
<radI:RadMaskedTextBox CssClass="box_SSN" SelectionOnFocus="SelectAll" ID="TaxId"
Width="85px" Mask="###-##-####" runat="server">
</radI:RadMaskedTextBox>
Try:
^\d{3}-\d{2}-\d{4}$
Instead of:
^\d{3}\-\d{2}\-\d{4}
This is working perfectly for me in IE:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{3}-\d{2}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid SSN!">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit"/>
Not sure why you have
Display="None"

ASP.NET - Get control in RowCommand event

Trying to access a RequiredFieldValidator control that's inside a GridView in the RowCommand event and having trouble.
Here's the partial GridView code:
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password" Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
As you can see, there's a RFV for the EditTemplate and FooterTemplate. My issue is this; when the page loads, it has all the records in it, including an emtpy row at the bottom (Footer). If I click Edit on a populated row, the data is populated correctly, then when I hit UPDATE, I get all the error messages from the FOOTER RFV's firing off, which isn't correct. So, in the RowCommand event, I'd like to attempt this: If the user clicks the EDIT button, then disable all the RFV's in the footer row (the add new row), if they click anything else, enable them.
Ideas?
Sorry, meant to put this in the first time. In the RowCommand event, I am able to find the control but when I set the properties to something bogus, it seems to get overridden later, by the RowDataBound event:
RequiredFieldValidator rfv = (RequiredFieldValidator)gvUsers.FooterRow.FindControl("rfvAddWebLogin");
rfv.ControlToValidate = string.Empty;
rfv.ErrorMessage = "sdfgsdfgsdgsdfgsdfgsdfgsdfg";
rfv.Enabled = false;
You should use different ValidationGroups in your EditItemTemplate and FooterItemplate:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="BtnEdit" CausesValidation="False" Text="Edit" CommandName="Edit" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" ValidationGroup="UpdateUser" Text="Update" CommandName="Update" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" ValidationGroup="InsertUser" Text="Add" CommandName="Insert" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password"
Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" ValidationGroup="UpdateUser" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" ValidationGroup="UpdateUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" ValidationGroup="InsertUser" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" ValidationGroup="InsertUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic7
Note: If you're using ValidationSummaries, you need to add the appropriate ValidationGroup to every ValidationSummary. If you leave this property blank, only controls without a specified ValidationGroup will be listed.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.validationgroup.aspx

How to show validation control's Error messages in Alert box?

I am using 4 required field validators,4 regular expression validators and 4 compare validators for 4 text boxes.Is it possible to show error messages
in an alert or message box when validation fails?
If possible please send code sample.
Regards,
NSJ
<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>
You should use the same ValidationGroup text on all validation controls and add a ValidationSummary with the ValidationGroup and ShowMessageBox="true"
Use the following code , just set the correct messeges that u want :
<asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox ="true"
runat="server" />
<asp:textbox id="txt1" runat="server"></asp:textbox>
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server"
controltovalidate="txt1" errormessage="Please Enter Only Numbers" validationexpression="^[-+]?\d+(\.\d+)?$">
</asp:regularexpressionvalidator>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt1"
errormessage="please enter txt1">*</asp:requiredfieldvalidator>
<asp:textbox id="txt2" runat="server"></asp:textbox>
<asp:regularexpressionvalidator id="RegularExpressionValidator2" runat="server"
controltovalidate="txt2" errormessage="Please Enter Only Charcters" validationexpression="^[a-zA-Z\s.]*$">
</asp:regularexpressionvalidator>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt2"
errormessage="please enter txt2">*</asp:requiredfieldvalidator>
<asp:textbox id="txt3" runat="server"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt3"
errormessage="please enter txt3">*</asp:requiredfieldvalidator>
<asp:Button ID="btnok" runat="server" Text="ok"/>
Don't do it. Users hate alert box error messages. It is a horrible UI design. Put the error messages on the form where the user can read them and they stay while the user makes changes or moves on without having to click on an alert box. Especially when there are multiple corrections to be made, they need to be able to read the errors while they make the corrections.
You can use this function
set parameters validation message and Control ID as parameters
protected void PopupMessage(string Msg, Control controlID)
{
ScriptManager.RegisterClientScriptBlock(controlID, controlID.GetType(), "msg", "alert('" + Msg + "');", true);
}
within the button click event you can this function according to your logic
protected void btnok_Click(object sender, EventArgs e)
{
if(TextBox1.Text=="")
PopupMessage("Name is Required", btnok);
}
please follow following code which is useful for me use ShowSummary property to reduce duplication of message into your pannel
<asp:ValidationSummary ID="VS1" ShowMessageBox="true" runat="server" ShowSummary="False" />
<asp:Label ID="lblUsername" runat="server" >User name</asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV1" runat="server" ControlToValidate="txtUserName" ErrorMessage="Please Select Name" Display="None" SetFocusOnError="True">
</asp:RequiredFieldValidator><br />

ValidationSummary shows all errors on one line

here is my code:
<asp:ValidationSummary ShowSummary="true" ID="ValidationSummary1" ValidationGroup="x" runat="server" />
<asp:RequiredFieldValidator ValidationGroup="x" ControlToValidate="ddlIso" ID="RequiredFieldValidator2" runat="server" Display="none" SetFocusOnError="true" ErrorMessage="<br>y is required"/>
<asp:RequiredFieldValidator ValidationGroup="x" ControlToValidate="txtIsoAct" ID="RequiredFieldValidator3" runat="server" Display="none" SetFocusOnError="true" ErrorMessage="<br>y Base Activity is required"/>
<asp:RequiredFieldValidator ValidationGroup="x" ControlToValidate="txtIsoDate" ID="RequiredFieldValidator4" runat="server" Display="none" SetFocusOnError="true" ErrorMessage="<br>y Base Activity Date is required"/>
Validation Summarys hows all three messages on one line... how can i get them to show up as bullets?
Use the DisplayMode property:
http://msdn.microsoft.com/en-us/library/dd5c6s6h%28v=vs.71%29.aspx

ASP.NET AJAX Control Toolkit ValidatorCallout not working on edit template

I have a page that has a listview that is used for inserting and editing records.
Assigning a RequiredFieldValidator and ValidatorCallOutExtender to the InsertItemTemplate works well.
When I try to do the same on the EditItemTemplate the ValidatorCallOut appears but with no text in the box.
Is there something that I'm doing wrong?
My code for the InsertItemTemplate:
<asp:TextBox ID="date_timeTextBox" runat="server" Text='<%# Bind("date_time") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="insert_into">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
runat="server"
TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
And for the EditItemTemplate:
<asp:TextBox
ID="date_timeTextBox"
runat="server"
Text='<%# Bind("date_time","{0:yyyy-MM-dd}") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="reqDTT"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="edit_validate">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender
ID="val_reqDTT"
runat="server"
TargetControlID="reqDTT">
</cc1:ValidatorCalloutExtender>
Make sure your ID's are unique across your Templates, so the ControlToValidate="date_timeTextBox" is different.
InsertTemplate
<asp:TextBox ID="date_timeTextBoxInsert" runat="server" Text='<%# Bind("date_time") %>' />
EditTemplate
<asp:TextBox ID="date_timeTextBoxEdit" runat="server" Text='<%# Bind("date_time") %>' />

Resources