This is an example of my textbox in my form with a custom validation, I want to disable the validators :
<asp:textbox id="txtFirstName" runat="server" /></td><td>
<script language ="javascript">
function requireFirstName(source, args) {
if (document.getElementById("<%=txtFirstName.ClientID %>").value =="") {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
</script>
<asp:CustomValidator ID="RequiredValidator1" runat="server"
ErrorMessage="You must enter your first name."
ForeColor="Red" clientvalidationfunction="requireFirstName"
></asp:CustomValidator>
This is my previous button:
<asp:Button ID="PreviousButton" runat="server"
Text="<-- Back to Instructions" OnClick="btnBack_Click"
class="previous" />
<script type="javascript">
document.getElementById("<%=PreviousButton.ClientID%>").disableValidation = true;
</script>
This will not work and still has validators..help
Try setting CausesValidation to false.
<asp:Button ID="PreviousButton" runat="server" Text="<-- Back to Instructions" OnClick="btnBack_Click" class="previous" CausesValidation="false" />
<asp:Button ID="PreviousButton" runat="server" CausesValidation="false"
Text="<-- Back to Instructions" OnClick="btnBack_Click"
class="previous" EnableClientScript="false" />
Related
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" />
I am using required field validator, which validates text box and on click of submit button i have to ask for confirm, and I use confirm() function of java script.
Issue is that when I press OK in confirmation box page post backs and required field validator does not stops the page when I left the text box empty.
After reading posts from stackoverflow I used custom validator to stop the page, but I could not here is the code.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
var cv = document.getElementById('MainContent_CustomValidator1');
if (cv) {
cv.isValid = confirm('are you sure want to update record ?');
}
} </script>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator" ClientValidationFunction="TextBox1Client"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
if(confirm('are you sure want to update record ?')){
return true;
}
else
{
return false;
}
}
//you need to add a custom validot client function also
function TextBox1Client(sender, args) {
//write your custom code here
args.IsValid = false;
//OR
args.IsValid =true;
}
</script>
try this code use Page_ClientValidate
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="Javascript:if(Page_ClientValidate('one')){return validate();}" ValidationGroup="one" onclick="Button1_Click"/>
correct your code like this
if(confirm('are you sure want to update record ?')==false){
return false;
}
Thanks.
JavaScript is case sensitive, try this:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
var cv = document.getElementById('MainContent_CustomValidator1');
if (cv) {
cv.IsValid = confirm('Are you sure want to update record?');
}
} </script>
You didn't capitalize the i in IsValid.
I have an .aspx page that contains two buttons one is "btnCancel" and another one is "btnSave".
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"
OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp"/>
Now the problem is after filling up some of the textboxes which are present in that page, if I press the "Enter button" the "btnCancel_Click" event is firing instead of "btnSave_Click".
Can any one please suggest me how to set the "btnSave_Click" button as the default one. So that if any one press the "enter button" it will fire the "btnSave_Click" event.
Any help please.
Updated Question:
<asp:Panel DefaultButton="btnSave" runat="server" ID="pnlTest">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"
OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp" />
</asp:Panel>
Set the defaultbutton attribute on the containing panel:
<asp:panel defaultbutton=“btnSave”>
Taken from this blogpost.
put your form controls inside asp.net Panel and set DefaultButton to your button id
Example:
<asp:Panel DefaultButton="btnSave">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp"/>
</asp:Panel>
you can simply use javascript
<script language="javascript" type="text/javascript" >
function button_click(objTextBox,objBtnID)
{
if(window.event.keyCode == 13)
{
document.getElementById(objBtnID).focus();
document.getElementById(objBtnID).click();
}
}
</script>
add this in your page load event
this.TextBox1.Attributes.Add("onkeypress", "button_click(this,'" + this.btnSave.ClientID + "')");
we can set the "defaultbutton" property in the form tag
set this javascript to header
function checkKey(b1, e) {
if (e.keyCode == 13) {
//alert(e.keyCode + 'validation.js');
document.getElementById(b1).click();
return false;
}
}
and set attributes to control
txtboxname.Attributes.Add("onkeypress", "javascript:return checkKey('" + buttonName.ClientID + "',event);");
I have a Radio button List and Text Box both with validation.
<asp:RadioButtonList ID="member" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:requiredfieldvalidator id="unionvalidator" runat="server" controltovalidate="member" errormessage="Required" />
Required if member == "yes"
<asp:TextBox runat="server" ID="union"></asp:TextBox>
<asp:customvalidator ID="Customvalidator1" runat="server" ValidateEmptyText="true" onServerValidate="UnionValidate" errormessage="Your current union is required" />
My ServerValidate which doesn't fire at all.
public void UnionValidate(object source, ServerValidateEventArgs args)
{
if (member.Text == "yes" && union.Text.Trim() == "")
args.IsValid = false;
}
Are you calling the Page.Validate() method somewhere in your code behind or does the submit button has the CausesValidation set to true?
On the page load i wanna call a custom validator which validates my page and display corresponding error messages.
How can i do this?
I am using javascript in the custom validator.
You can use Page.IsValid and Page.Validate()
http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx
After Edit: // Here is a working sample:
JS:
<script type="text/javascript">
function EmpIDClientValidate(ctl, args) {
// the value is a multiple of 5 if the module by 5 is 0
args.IsValid = (args.Value % 5 == 0);
}
</script>
Aspx:
<table>
<tr>
<td>
ID (multiple of 5):
</td>
<td>
<asp:TextBox runat="server" Width="200px" ID="EmpID" Text="12"/>
<asp:RequiredFieldValidator runat="server" ID="ValidateEmpID" ControlToValidate="EmpID"
ErrorMessage="ID is required" Display="dynamic">*
</asp:RequiredFieldValidator>
<asp:CustomValidator runat="server" ID="ValidateEmpID2" ControlToValidate="EmpID"
ClientValidationFunction="EmpIDClientValidate" ErrorMessage="ID must be a multiple of 5"
Display="dynamic" OnServerValidate="ValidateEmpID2_ServerValidate">*
</asp:CustomValidator>
</td>
</tr>
</table>
<br />
<asp:Button runat="server" Text="Submit" ID="Submit" OnClick="Submit_Click" /><br />
<br />
<asp:CheckBox runat="server" ID="chkEnableValidators" Checked="True" AutoPostBack="True"
Text="Validators enabled" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkEnableClientSide" Checked="True" AutoPostBack="True"
Text="Client-side validation enabled" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkShowSummary" Checked="True" AutoPostBack="True"
Text="Show summary" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkShowMsgBox" Checked="False" AutoPostBack="True"
Text="Show message box" OnCheckedChanged="OptionsChanged" />
<br />
<br />
<asp:ValidationSummary runat="server" ID="Summary" DisplayMode="BulletList" HeaderText="<b>Please review the following errors:</b>"
ShowSummary="true" />
<asp:Label runat="server" ID="Result" ForeColor="magenta" Font-Bold="true" EnableViewState="False" />
And codebehind:
protected void Page_Load(object sender, EventArgs e)
{
// To run all validators
Page.Validate();
// To run just one validator:
ValidateEmpID2.Validate();
}
protected void Submit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
Result.Text = "Thanks for sending your data";
else
Result.Text = "There are some errors, please correct them and re-send the form.";
}
protected void ValidateEmpID2_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
args.IsValid = (int.Parse(args.Value) % 5 == 0);
}
catch
{
args.IsValid = false;
}
}
protected void OptionsChanged(object sender, EventArgs e)
{
// Examine all the validators on the back.
foreach (BaseValidator validator in Page.Validators)
{
// Turn the validators on or off, depending on the value
// of the "Validators enabled" check box (chkEnableValidators).
validator.Enabled = chkEnableValidators.Checked;
// Turn client-side validation on or off, depending on the value
// of the "Client-side validation enabled" check box
// (chkEnableClientSide).
validator.EnableClientScript = chkEnableClientSide.Checked;
}
// Configure the validation summary based on the final two check boxes.
Summary.ShowMessageBox = chkShowMsgBox.Checked;
Summary.ShowSummary = chkShowSummary.Checked;
}
Page.Validate(), assuming you are using the CustomValidator server control
You can call the function as
<body onload ="Functionname()">
In case of javascript can use as onload="javascript:functionname();"