I am using CustomValidator for Telerik RadMaskedTextbox.The problem is that if i don't put any value , it doesn't show errormessage.
<telerik:RadMaskedTextBox ID="RadMaskedTextBox3" runat="server"
Width="150"
Mask="(###) ###-#### ext. #####">
</telerik:RadMaskedTextBox>
<asp:CustomValidator ID="CustomValidator4" runat="server"
ErrorMessage="*"
Display="Dynamic"
CssClass="error1"
Enabled="false"
ToolTip="At least one Phone no: needs to be filled in."
ValidateEmptyText="true"
EnableClientScript="true"
OnServerValidate="CustomValidator_ServerValidate"
SetFocusOnError ="true"
ValidationGroup="CarrierBaseInformation1">
</asp:CustomValidator>
Here you are another example with a CustomValidator:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="CheckLength"
ErrorMessage="Phone\Fax numbers must be 7 or 9 digits"
ControlToValidate="txtTollFree">*</asp:CustomValidator>
<script>
function CheckLength(source, args)
{
if (args.Value.length == 10 || args.Value.length == 13)
{
args.IsValid = true;
}else{
args.IsValid = false;
}
}
</script>
Here is an example how to achieve your goal:
In the Web.config set
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
In the aspx
<telerik:RadMaskedTextBox Mask="(###) ###-#### ext. #####" RenderMode="Lightweight" ID="RadMaskedTextBox1" runat="server" EmptyMessage="Enter username"></telerik:RadMaskedTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ControlToValidate="RadMaskedTextBox1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="PostBack" />
Related
I'm trying to force the user to choose to fill either the Photo or the Video Textbox using the CustomValidator but it's not working, I've tried searching around and from previous questions a lot of people instructed to add the ValidateEmptyText="true" property, I tried adding it but it still won't fire.
I'm using other RequiredFieldValidators which are operating normally.
This is my aspx code of the two fields:
<asp:Button ID="btn1" runat="server" Text="+"/>
<asp:TextBox runat="server" PlaceHolder="Photos" ID="pics" ValidationGroup="txt1"></asp:TextBox>
<br />
<asp:Button ID="btn2" runat="server" Text="+"/>
<asp:TextBox ID="vids" runat="server" PlaceHolder="Videos" ValidationGroup="txt1"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidationGroup="txt1" ValidateEmptyText="true"></asp:CustomValidator>
This is my c# Validation method:
public void ValidateBoxes(object sender, ServerValidateEventArgs e)
{
if (string.IsNullOrEmpty(pics.Text) && string.IsNullOrWhiteSpace(vids.Text))
e.IsValid = false;
else
e.IsValid = true;
}
EDIT : This is one of the text boxes and it's validators from the output screen shots.
<asp:TextBox ID ="city_in" PlaceHolder ="Enter city" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="city_in" ErrorMessage="Please enter the city!" ForeColor="Red"></asp:RequiredFieldValidator>
EDIT: This is the whole aspx Code:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h1>
Creating An Event
</h1>
<br />
<h3>
Please Provide the information below
</h3>
<asp:TextBox ID ="city_in" PlaceHolder ="Enter city" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="city_in" ErrorMessage="Please enter the city!" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<asp:TextBox ID="date" runat="server" PlaceHolder ="Enter date" TextMode="Date" ></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="date" ErrorMessage="Please enter the date!" ForeColor="Red" ></asp:RequiredFieldValidator>
<br />
<br />
<asp:TextBox ID="desc" runat="server" PlaceHolder = "Description"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="desc" ErrorMessage="Please enter the description!" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<asp:TextBox ID ="entertain" runat="server" PlaceHolder ="Entertainer"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="entertain" ErrorMessage="Please enter the entertainer!" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<asp:TextBox ID ="viewer" runat="server" PlaceHolder ="ID"></asp:TextBox>
<br />
<br />
<asp:TextBox ID ="location" runat="server" PlaceHolder ="Location"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter the location!" ControlToValidate="location" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<p>
Please choose what type of Multimedia you would like to upload
</p>
<br />
<asp:Button ID="btn1" runat="server" Text="+"/>
<asp:TextBox runat="server" PlaceHolder="Photos" ID="pics" ></asp:TextBox>
<br />
<asp:Button ID="btn2" runat="server" Text="+"/>
<asp:TextBox ID="vids" runat="server" PlaceHolder="Videos"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidateEmptyText="true"></asp:CustomValidator>
<br />
<br />
<asp:Button ID ="btn" runat="server" Text="Create Event" OnClick="create_Event" />
<asp:Button runat="server" Text="Cancel" OnClick="go_Profile"/>
Output:
This code was tested and works properly.
<body>
<form id="form1" runat="server">
<p>
Please choose what type of Multimedia you would like to upload
</p>
<br />
<asp:TextBox runat="server" PlaceHolder="Photos" ID="pics"></asp:TextBox>
<br />
<asp:TextBox ID="vids" runat="server" PlaceHolder="Videos"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidateEmptyText="true"></asp:CustomValidator>
<br />
<br />
<asp:Button ID="btn" runat="server" Text="Create Event" />
<asp:Button runat="server" Text="Cancel" />
</form>
</body>
with this code:
public void ValidateBoxes(object sender, ServerValidateEventArgs e)
{
if (string.IsNullOrEmpty(pics.Text) && string.IsNullOrWhiteSpace(vids.Text))
e.IsValid = false;
else
e.IsValid = true;
}
if I enter any value in either of the two textboxes, the Validator is not shown.
I wanted to leave a comment but figured it would be best to display to you exactly what I tested this way you know what is working.
You have to make sure the Page IsValid before creating your event...
protected void btn_Click(object sender, EventArgs e)
{
if (IsValid)
{
Response.Write("Creating an event");
}
}
My code is like this
<asp:TextBox ID="txtStartDate" CssClass="txtStartDate" runat="server" MaxLength="10" />
<asp:RequiredFieldValidator ID="startDateRequiredFieldValidator" runat="server" ValidationGroup="Dates" ControlToValidate="txtStartDate"
EnableClientScript="True" Display="None" Text="*" ErrorMessage="Start date is required."/>
<asp:CompareValidator ForeColor="Red" id="startDateCompareValidator1" runat="server" Type="Date"
ValidationGroup="Dates" Display="None" EnableClientScript="True"
Operator="DataTypeCheck" ControlToValidate="txtStartDate" Text="*"
ErrorMessage="Start date is not valid or is in an incorrect format. Please use the format yyyy-MM-dd."/>
<asp:RangeValidator id="ReturnDateRangeValidator" runat="server" ControlToValidate="txtStartDate" ValidationGroup="Dates"
MinimumValue="2005-01-01" MaximumValue="2050-01-01" Display="None" EnableClientScript="True" Text="*"
ErrorMessage="Start date is too far back in time or it is to far in future, please enter a more feasible date."/>
<cc1:CalendarExtender ID="Calendarextender2" runat="server" Format="yyyy-MM-dd" PopupButtonID="Image2"
TargetControlID="txtStartDate" FirstDayOfWeek="Monday">
</cc1:CalendarExtender>
I have a date field and I can use delete,backspace into IE but I can't do it into Chrome. My question is how can I enable backspace,delete into Chrome . Any info will be helpful regarding this
You Can Use JQuery For this
here is your textbox with event keypress
<asp:TextBox ID="txtStartDate" CssClass="txtStartDate" onkeypress="return allowBackSpace(this);" runat="server" MaxLength="10" />
Some Script
function allowBackSpace(val) {
var keyCodeEntered = (event.which) ?
event.which :
(window.event.keyCode) ?
window.event.keyCode :
-1;
if (keyCodeEntered == 8) {
$(this).val("");
return false;
}
return false;
}
I am trying to disable a RequiredFieldValidator by radio button list value. I have it coded but it doesn't seems to be working.
What i have done so far:
protected void radioButtonList(object sender, EventArgs e)
{
if (((RadioButtonList)dvInsertPromotion.FindControl("RadioButtonList2")).SelectedValue == "Y")
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = false;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = false;
this.addPromotion.Show();
}
else
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = true;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = true;
this.addPromotion.Show();
}
}
html:
<asp:RadioButtonList ID="RadioButtonList2" runat="server" ValidationGroup="addValidationGp" OnSelectedIndexChanged="radioButtonList">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N" Selected></asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtPubDate" Width="75" MaxLength="10" runat="server" AutoPostBack="true" OnTextChanged="insertStartEndDateTime_SelectedIndexChanged"/>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPubDate"
PopupPosition="Right" Format="dd/MM/yyyy" />
<asp:RequiredFieldValidator ID="rfvdate" ValidationGroup="addValidationGp" runat="server"
ControlToValidate="txtPubDate" ErrorMessage="*" Display="Dynamic" Enabled="true"
SetFocusOnError="true" /><br />
I've had the same problem in the past and the only way I was able to solve this was by making the validators invisible altogether:
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Visible = false;
Just don't forget to set them to visible again whenever you want to enable them.
In the past, on button click events, I've validated without using RequiredFieldValidators. However, I thought I'd learn about them and implement them.
My old approach:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtSubject.Text.Equals("") || txtEmail.Text.Equals("") || txtComments.Text.Equals(""))
{
lblMessage.Text = "Please check all fields have been entered.";
}
//else if ...further validation statements e.g. check lengths
}
However, using RequiredFieldValidators with the same example, am I correct in saying that I don't have to check again if (txtSubject.Text.Equals("") || txtEmail.Text.Equals("") || txtComments.Text.Equals("")) like below or is it good practice to do so?
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//...further validation statements e.g. check lengths
try
{
SendMail();
}
catch (Exception)
{
}
}
}
If I should still include the line, it should go at the beginning of the if (Page.IsValid), right?
HTML code:
<p>Contact Form</p>
<p>
Your name:
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="*"
ControlToValidate="txtName" ValidationGroup="save" /><br />
<asp:TextBox ID="txtName" runat="server" Width="250px" /><br />
Your email address:
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*"
ControlToValidate="txtEmail" ValidationGroup="save" /><br />
<asp:TextBox ID="txtEmail" runat="server" Width="250px" />
<asp:RegularExpressionValidator runat="server" ID="rfvEmail2"
SetFocusOnError="true" Text="Example: email#gmail.com" ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
ValidationGroup="save" /><br />
Subject:
<asp:RequiredFieldValidator ID="rfvSubject" runat="server" ErrorMessage="*"
ControlToValidate="txtSubject" ValidationGroup="save" /><br />
<asp:TextBox ID="txtSubject" runat="server" Width="400px" /><br />
Comments:
<asp:RequiredFieldValidator ID="rfvComments" runat="server" ErrorMessage="*"
ControlToValidate="txtComments" ValidationGroup="save" /><br />
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" ValidationGroup="save" />
</p>
<p>
<asp:Label ID="lblMessage" runat="server" Visible="true" />
</p>
why dont you do the following?
Page.Validate("save");
if (Page.IsValid)
{
//Continue with your logic
}
else
{
//Display errors, hide controls, etc.
}
This only fires your validation group and furthermore , you can use a validation summary to display your message about the correct formats of the text boxes.
And you can display an error message then and there to display the correct format.
I am designing a web-application using asp.net with c# and I just added a validation code for a textbox,it seems like it is good enough to execute,but no validation issues is been shown when the application is executed when input is null or invalid.
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" DisplayMode="BulletList"
HeaderText="Validation issues" ShowSummary="False" ValidationGroup="Validation"/>
<asp:TextBox ID="txtrandom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Enter the Randomly generated numbers"
ControlToValidate="txtrandom" Display="None"
ValidationGroup="Validation" SetFocusOnError="true" >
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ErrorMessage="Input should be in number"
ValidationExpression="^[0-9]+$"
ControlToValidate="txtrandom"
Display="None"
ValidationGroup="Validation"
SetFocusOnError="true" >
</asp:RegularExpressionValidator>
In the backend(c#) I have these line of code
int random = 0;
bool isValidInt = int.TryParse(txtrandom.Text, out random);
for (int i = 0; i < random; i++)
{
//other codes
}
Does these lines of code effect the validation or just a syntactical error? Any help is appreciated.As far as i know he text box is taking 0 as a default value.
The problem is with the validation group . If you are not using the validation group everything will work but if you specified a validation group then the group has to be enabled in the button click event or something similar.
see my code . it is working fine.
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" DisplayMode="BulletList"
HeaderText="Validation issues" ShowSummary="false" ValidationGroup="one" />
<asp:TextBox ID="txtrandom" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Enter the Randomly generated numbers" Display="None"
ControlToValidate="txtrandom" ValidationGroup="one" >
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ErrorMessage="Input should be in number"
ValidationExpression="^[0-9]+$"
ControlToValidate="txtrandom"
Display="None"
ValidationGroup="one"
SetFocusOnError="true" >
</asp:RegularExpressionValidator>
<asp:Button ID="test" runat="server" Text="Submit" ValidationGroup="one" />
</div>
and yes validation group can be invoked on the post back.So error message wont display onfocouschange just like the normal validation.
Assign Validation group to textbox like this:
<asp:TextBox ID="txtrandom" runat="server" ValidationGroup="Validation"></asp:TextBox>
You can Try this Code, I am also using this code of numeric checking
bool isnum;
double numericval;
isnum = double.TryParse(numval, out numericval);
if (isnum)
{ return true; }
else { return false; }