I am trying to set MaxValue of MaskedEditValidator which is of date type but it is not Validating on the MaxValue after click on Submit Button. Below Is my HTML Code for Masked Edit Validator
<asp:TextBox ID="txtLectureDate" runat="server" class="form-control" placeholder="Please Select End Date" TabIndex="3" OnTextChanged="txtEndDate_TextChanged" ValidationGroup="Submit" AutoPostBack="true">
</asp:TextBox>
<ajaxtoolkit:CalendarExtender ID="cldrexEndDate" runat="server" TargetControlID="txtLectureDate" Format="dd/MM/yyyy" PopupButtonID="imgECal"
CssClass=" cal_Theme1">
</ajaxtoolkit:CalendarExtender>
<ajaxtoolkit:MaskedEditExtender ID="meeEndDate" runat="server" TargetControlID="txtLectureDate" Mask="99/99/9999" MaskType="Date" AcceptNegative="Left" MessageValidatorTip="false" InputDirection="LeftToRight">
</ajaxtoolkit:MaskedEditExtender>
<ajaxtoolkit:MaskedEditValidator ID="mevEndDate" runat="server" ControlToValidate="txtLectureDate" ControlExtender="meeEndDate" IsValidEmpty="false" EmptyValueMessage="Please Enter End Date" ErrorMessage="Please Enter Valid Date in [dd/MM/yyyy]" InvalidValueMessage="Please Enter Valid Date" MaximumValueMessage ="Attendance Date Should Not Be Greater Than Current Date" ValidationGroup="Submit" Display="None">
</ajaxtoolkit:MaskedEditValidator>
And below is my Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
mevEndDate.MaximumValue = DateTime.UtcNow.AddHours(5.5).ToString("dd/MM/yyyy");
}
}
Please Help Me in this.
Please ensure that MaskedEdit.UserDateFormat="DayMonthYear". Also I noticed, that validation occurs only on textbox blur.
Related
I have calculated the two textbox values and displayed successfully it into third textbox but the issues are when I enter value to textbox1 and move to textbox2 the page get reload as I have mentioned AutoPostBack="true" for textbox1 and textbox2 and when I remove this AutoPostBack="true" the calculated value is not displayed in third textbox.
Here is my code behind
protected void txttotal_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
protected void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
Here is aspx code
<asp:TextBox ID="txttotal" runat="server" CssClass="textstyle" OnTextChanged="txttotal_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtdiscount" runat="server" CssClass="textstyle" OnTextChanged="txtdiscount_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtgrandtotal" runat="server" CssClass="textstyle" ReadOnly="true"></asp:TextBox>
To add the two text box value and show it on third text box you can add them on button click event here is the code
aspx page
<div>
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<br />
<asp:Label runat="server" Text="Answer"></asp:Label> <asp:TextBox runat="server" ID="txt3"></asp:TextBox>
<asp:Button runat="server" ID="btn1" Text="CLICK TO ADD" OnClick="btn1_Click"/>
</div>
.cs
protected void btn1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txt1.Text) && !string.IsNullOrEmpty(txt2.Text))
{
txt3.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text)).ToString();
}
else {
Response.Write("Please Enter Value");
}
}
I am trying to apply CssClass when validator is false, it worked for me in the past and now I don't know why it doesn't work.
Textbox:
<asp:TextBox ID="txtPass"
AutoPostBack="false"
ClientIDMode="Static"
runat="server"
placeholder="Password (8 - 16 digits)"
CssClass="Text"
type="password"
ValidationGroup="check">
</asp:TextBox>
Validators attached to this textbox:
<asp:RegularExpressionValidator ID="revPass"
runat="server"
Display="None"
ControlToValidate="txtPass"
EnableClientScript="false"
ValidationExpression="[a-zA-Z0-9]{8,16}"
ValidationGroup="check">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvPass"
runat="server"
EnableClientScript="false"
ControlToValidate="txtPass"
Display="None"
ValidationGroup="check">
</asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvPasswords"
runat="server"
ErrorMessage="Passwords do not match!"
EnableClientScript="false"
CssClass="error"
ControlToCompare="txtPass"
ControlToValidate="txtConPass"
ValidationGroup="check">
</asp:CompareValidator>
But I have to say that only the last one works. The validators work, just not changing CssClass.
EDIT:
It is not even enteres the if statemant I checked it. there is the if statement:
if (!(revPass.IsValid) || !rfvPass.IsValid)
{
txtPass.CssClass = "txtError";
}
If you want to Apply Css on your Validator's Error messages Then you have to set CssClass Attribute of all Validators..
UPDATE :Sorry about that, i misunderstood your Question. Did you try this On Code behind
protected void Button1_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
// code executed when validation are valid
}
else
{
txtPass.CssClass = "Your New Class";
txtConPass.CssClass = "Your New Class";
}
}
The drop down list contains three countries, How do you set the minimum number of digits to be entered in the postcode text-box based on what country is selected.
For example, if user selects America, then the postcode text-Box should be set to "[0-9]{6}". But if they select Australia then the post code should be "[0-9]{4}".
<asp:DropDownList ID="dropDownList" runat="server">
<asp:ListItem Value="-1">Country</asp:ListItem>
<asp:ListItem>America</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
<asp:ListItem>Sweden</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidateCountry"
initialValue="-1"
ForeColor="Red"
runat="server"
ErrorMessage="Please choose Country"
ControlToValidate="dropDownList"/>
<asp:Label ID="Label1" runat="server" Text="PostCode"></asp:Label>
<asp:textbox id="TextBox1" runat="server" ></asp:textbox>
<asp:CustomValidator id="CustomValidator1" runat="server"
OnServerValidate="TextValidate"
ControlToValidate="TextBox1"
ErrorMessage="Invalid postcode.">
</asp:CustomValidator>
<asp:Button ID="BtnNext" runat="server" Text="Next" />
You can do this on the SelectedIndexChanged events of the dropdownlist. Set the AutoPostback to true in the dropdownlist write the similar described logic according to your need.
private void dropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
//your logic, something like that
if(dropDownList.SelectedText =="America")
{
TextBox1.MaxLength= your value ...
}
}
<asp:DropDownList ID="dropDownList" runat="server" AutoPostback = "true">
EDIT:
You can check the total number of character of textbox with your predefined value.
int minlength = 5;
if(textbox1.text.length < minlength)
{
//Flag error on validation label
}
else
{
//your stuff
}
You also have client side events for the asp .net dropdown. You can set a javascript function to execute if the dropdown selection changes without posting back to the server. This will make the experience more streamlined and you can alter the min and max length directly with jquery.
You can do it like this:
DropDownList1.Attributes.Add("onChange", "return OnSelectedIndexChange();")
I have some asp code written for a form. One of the fields must have 3 types of validation, 2 of which are alway enabled, the other 2 must be "enabled" only if a peculiar value has been selected from a dropdownlist.
How am I supposed to achieve this task?
I have disabled the 2 extra validations by defaults and wold like to reactivate on dropdownlist specific selection.
I show you my code here.
ASP.NET code:
<tr>
<td class="style1">
<asp:Label ID="LabelPiva" runat="server" Text="Partita IVA" meta:resourcekey="LabelPiva" Font-Bold="True" />
</td>
<td>
<asp:TextBox ID="pivaTextBox" runat="server" Text='<%# Bind("piva") %>'
MaxLength="50" Width="400px" />
<asp:RequiredFieldValidator ID="RequiredPiva" runat="server"
ControlToValidate="pivaTextBox"
ErrorMessage="<%$ Resources:Resource, CampoObbligatorio %>" Display="Dynamic"
CssClass="little_text" />
<asp:CustomValidator ID="PivaEsistente" runat="server"
ErrorMessage="Partita IVA esistente nel database" meta:resourcekey="PivaEsistente"
ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic"
onservervalidate="PIVAEsistente_ServerValidate"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="PivaSize" runat="server"
ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic"
ErrorMessage="Controllare la lunghezza della partita iva. 11 caratteri e solo numeri."
ValidationExpression="^[0-9]{11}$" Enabled="False" ValidationGroup="soloItalia">
</asp:RegularExpressionValidator>
<asp:CustomValidator ID="PivaErrata" runat="server"
ErrorMessage="Partita IVA non corretta. Controllare le cifre." meta:resourcekey="PivaErrata"
ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic"
onservervalidate="ValidatePI" Enabled="False" ValidationGroup="soloItalia"></asp:CustomValidator>
</td>
</tr>
The CustomValidator with id PivaErrata and the RegularExpressionValidator with id PivaSize must be fired only when dropdownlist hits the "it" value.
This is the code-behind in c# to intercept the value of the dropdownlist:
protected void nazioneDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DropDownList ddlProv = (DropDownList)FormUser.FindControl("provinciaDropDownList");
if ("it".Equals(ddl.SelectedValue))
{
ddlProv.Enabled = true;
}
else
{
ddlProv.SelectedIndex = 0;
ddlProv.Enabled = false;
}
}
As you can see, another dropdownlist gets enabled when that specific value "it" is fired in the nations dropdownlist.
I would like to activate the validations controls too.
I assigned a validationgroup to those 2 validations, but I am not sure how to "enable" them at once.
Any help?
Thank you very much.
you can use Enabled property of the Validation controls to enable or disable them.
Try This:
if (ddl.SelectedItem.ToString().Equals("it"))
{
ddlProv.Enabled = true;
//add these two statements to enable
PivaSize.Enabled=true;
PivaErrata.Enabled=true;
}
else
{
ddlProv.SelectedIndex = 0;
ddlProv.Enabled = false;
//add these two statements to disable
PivaSize.Enabled=false;
PivaErrata.Enabled=false;
}
I am trying to utilize a CustomValidator using the keyword "OnServerValidate"
Here is where I have it setup in my .aspx file:
<div class="label">
<div><label for="parentemail1">Email Address</label></div>
<div><asp:TextBox ID="parentemail1" runat="server" MaxLength="40" Columns="40"></asp:TextBox></div>
<asp:CustomValidator id="ParentEmail1Required"
ControlToValidate="parentemail1"
Display="Dynamic"
ErrorMessage="Required"
ValidateEmptyText="true"
OnServerValidate="ServerValidator"
runat="server"/>
</div>
and here is the C# code behind method for OnServerValidate:
protected void ServerValidator(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
Am I missing something, shouldn't the CustomValidator fire when the form is submitted?
I've read through a bunch of posts and even tried this with a RequiredFieldValidator on the same control but no luck. I have this working with ClientValidationFunction but I want to access the properties in the code behind instead of the DOM.
Any help would be greatly appreciated.
Try this
protected void ServerValidator(object source, ServerValidateEventArgs args)
{
if(args.IsValid == false)
return;
Response.Redirect("NextPage.aspx");// It will not fire if page is not valid.
}
Well, the argument args is not sent to server and that's why you don't get server message. Add a button can do this.
Change the code to
<div>
<div class="label">
<div><label for="parentemail1">Email Address</label></div>
<div><asp:TextBox ID="parentemail1" runat="server" MaxLength="40" Columns="40"></asp:TextBox>
<br />
<asp:Button ID="btnSendData" runat="server" Text="Send Data" />
</div>
<asp:CustomValidator id="ParentEmail1Required"
ControlToValidate="parentemail1"
Display="Dynamic"
ErrorMessage="Required"
ValidateEmptyText="true"
OnServerValidate="ServerValidator"
runat="server" SetFocusOnError="True"/>
</div>
</div>