Is it possible to have two ValidationSummary controls. One to display popup errors for some fields and one to display the standard summary for other fields?
Thanks
Yes, you can group validation using the ValidationGroup property on the form controls and the Summary control.
EDIT:
Using jQuery you could do something like this:
var validators = jQuery('.dataEntryFormTable').find("span[controltovalidate]");
validators.each(function()
{
var validatorEnabled = true;
if (jQuery('#' + this.id).attr('enabled') == false) {
validatorEnabled = false;
}
if (validatorEnabled)
{
// Check if Valid
// then Get Mesage Text, assign to list of messages to display
}
}
Related
Basically I want that the Title field(Combo Box) should not allow me to enter more than 40 characters.
Can you provide any pointers?
I looks like the control itself does not have that functionality, so you will probably have to write your own version.
You could create a custom control to extend the ComboxBox control. Check out this blog post.
Another idea is to use jQuery to prevent more than 40 characters from being added to the input control the ComboBox control generates:
$(function() {
var comboxBoxControlInput = $("#<%=comboBoxControlId.ClientID%>$TextBox");
$(comboxBoxControlInput).keyup(function() {
limitLenth(this, 40);
});
});
function limitLength(control, length) {
var currentContent = $(control).val();
var currentLength = currentContent.length;
if(currentLength > length) {
$(control).val(currentContent.substr(0, length));
return false;
}
}
Unfortunately it's a bit hacky. You have to get the ClientID of the ComboBox control (<%=comboBoxControlId.ClientID%>) and then append $TextBox to the end in order for jQuery to select the correct control.
Edit:
Another way to select the correct input control is to do this:
$("#<%=comboBoxControlId.ClientId%>").find("input[type=text]");
This selects the first text input within the div the ComboBox control creates.
I am creating one web application in which I am showing watermark text for some fields.
For example: I have one text box "Name" in which I am showing "Fill Name" as watermark. I have one required field validator for this text box. So, I am clearing the text box before the required field validator chck the text box other wise it will treat "Fill Name" as valid data and it will not apply validations. But when the validations fails, the watermark is not coming. Can we register some kind of callback method on asp.net validators?
I just want to run one java script function when asp.net validations fails.
Thanks
Ashwani
Have a look at the TextboxWatermarkextender from the AjaxControlToolkit. That controls fulfills all your described wishes. Have a look at the InitialValue property.
I got the solution for this.
function HookFunction() {
var Original_ValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = function (val) {
Original_ValidatorUpdateDisplay(val);
if (val) {
val.style.display = val.isvalid ? "none" : "inline-block";
}
};
var Original_ValidatorSetFocus = ValidatorSetFocus;
ValidatorSetFocus = function (val, event) {
if (val) {
ClearWaterMarkText($('#' + $(val).attr('controltovalidate')));
}
Original_ValidatorSetFocus(val, event);
};
}
I have a textbox that I want to change text property of it with javascript, but I can't do.
My sample code below, Can anyone say what is wrong? Thanks...
function openAdresYeni(p) {
document.getElementById('hdnAdresIndex').innerText = p;
}
}
Try this :
function openAdresYeni(p) {
document.getElementById('hdnAdresIndex').value = p;
}
NOTE : By the way if your hdnAdresIndex is a server control, you should use control's ClientID property to get client side id :
function openAdresYeni(p) {
document.getElementById('<%= hdnAdresIndex.ClientID %>').value = p;
}
use value instead of innerText
also, if you're not in asp.net mvc, the ID of the control is probably not what you expect. Look into the myTextBox.ClientID property on the asp.net control.
I am trying to disable validators using jquery.
I have already looked
Disable ASP.NET validators with JavaScript
and couple of others doing the same.
It seems ot be working but its breaking.
My code:
$('.c_MyValdiators').each(function() {
var x = $(this).attr('id');
var y = document.getElementById(x);
ValidatorEnable(y[0], false);
});
I get Error:
val is undefined
[Break on this error] val.enabled = (enable != false);\r\n
Alternatively if I use
$('.c_MyValdiators').each(function() {
ValidatorEnable($(this), false); OR ValidatorEnable($(this[0]), false);
});
I get Error:
val.style is undefined
[Break on this error] val.style.visibility = val.isvalid ? "hidden" : "visible";\r\n
Any idea or suggestions?
I beleive that ValidatorEnable takes the ASP.net ID rather that the ClientID produced by ASP.net. You will also need to make the validation conditional in the CodeBehind.
here is an example:
Of particular use is to be able to enable or disable validators. If you have validation that you want active only in certain scenarios, you may need to change the activation on both server and client, or you will find that the user cannot submit the page.
Here is the previous example with a field that should only be validated when a check box is unchecked:
public class Conditional : Page {
public HtmlInputCheckBox chkSameAs;
public RequiredFieldValidator rfvalShipAddress;
public override void Validate() {
bool enableShip = !chkSameAs.Checked;
rfvalShipAddress.Enabled = enableShip;
base.Validate();
}
}
Here is the client-side equivalent:
<input type=checkbox runat=server id=chkSameAs
onclick="OnChangeSameAs();" >Same as Billing<br>
<script language=javascript>
function OnChangeSameAs() {
var enableShip = !event.srcElement.status;
ValidatorEnable(rfvalShipAddress, enableShip);
}
</script>
Reference: http://msdn.microsoft.com/en-us/library/aa479045.aspx
I just stumbled upon your Question [a year later].
I too wanted to disable all validators on a page using JQuery here is how I handled it.
$('span[evaluationfunction]').each(function(){ValidatorEnable(this,false);});
I look for each span on the page that has the evaluatefunction attribute then call ValidatorEnabled for each one of them.
I think the $('this') part of your code is what was causing the hickup.
ValidatorEnable(document.getElementById($(this).attr('id')), true);
I've got another solution, which is to use the 'enabled' property of the span tag for the validator. I had different divs on a form that would show or hide so I needed to disable the validation for the fields inside the hidden div. This solution turns off validation without firing them.
If you have a set of RequiredFieldvalidator controls that all contain a common string that you can use to grab them the jquery is this:
$("[id*='CommonString']").each(function() {
this.enabled = false; // Disable Validation
});
or
$("[id*='CommonString']").each(function() {
this.enabled = true; // Enable Validation
});
Hope this helps.
John
I'm just running into the same problem, thanks to the other answers, as it helped uncover the problem, but they haven't gone into detail why.
I believe it is due to that ValidatorEnable() expects a DOM object (i.e. the validation control object) opposed to an ID.
$(selector).each() sets "this" to the DOM element being currently iterated over i.e. quoted from the jquery documentation:
"More importantly, the callback is fired in the context of the current
DOM element, so the keyword this refers to the element." - http://api.jquery.com/each/
Therefore you do not need to do: document.getElementById($(this).attr('id')
And instead ValidatorEnable(this, true); is fine.
Interestingly, Russ's answer mentioned needing to disable server side validation as well, which does make sense - but I didn't need to do this (which is concerning!).
Scrap my previous comment, it is because I had my control disabled server-side previously.
The ValidatorEnable function takes an object as the 1st parameter and not a string of the id of the object.
Here is the simple way to handle this.
Add a new class to the Validation control.
Then look for that class with jquery and disable the control.
Example :
if (storageOnly == 1)
{
$('#tblAssignment tr.assdetails').addClass('hidden');
$('span[evaluationfunction]').each(function ()
{
if ($(this).hasClass('assdetail'))
{ ValidatorEnable(this, false); }
});
}
else
{
$('#tblAssignment tr.assdetails').removeClass('hidden');
}
* Works like a charm.
** For you imaginative types, assdetail == assignment detail.
Here depending on the if condition, I am either hiding the rows then disabling the validator , or removing hidden class from the rows..
Various ways to this depending on your needs. Some solutions in the following blog posts:
http://imjo.hn/2013/03/28/javascript-disable-hidden-net-validators/
http://codeclimber.net.nz/archive/2008/05/14/How-to-manage-ASP.NET-validation-from-Javascript-with-jQuery.aspx
I have an asp.net page, some of its controls are created dynamically, these controls are one of the following; textbox, calendar, or dropdownlist.
These controls in some cases, should be validated based on flag read from db?
Is there any way to validate
dynamically created controls?
Basically you will need to create your validators via code and attach them to the dynamically created controls via code too. The page will then render with your validators in the page for you.
If validation requires a flag to be read from the db then perhaps use a custom validator which will allow you to set up your specific logic on both the client and server for your specific validation requirements. You don't have to provide client validation if you don't want to.
You can create validators at the same time you create those controls
When you are creating any control dynamically, also attach with them desired Validator control and you can enable/disable validator controls at run time.
I got a solution for that problem.
One of the main problems that I had with this page that it's ajax-enabled and I need to validate dynamically created controls.
My solution and it works properly, while creating the Control, I added an input attribute to it markes it as it is required or not, and another attribute that marks it as it is a field to be validated or not?
Using Javascript, I go through all input tags with attribute "dynamic control" and based on "to validate attribut", I validate it or not. Simple, right?
Sample Code:
While control creating, mark it like the following
txtBox.Attributes.Add("Type", "T"); // Type of control.
txtBox.Attributes.Add("IsKeyField", "Y"); // Is dynamically created field.
txtBox.Attributes.Add("IsMandatory", "Y"); // Is mandatory or not?
JavaScript code
var inputControls = document.getElementsByTagName("input");
for(var i=0 ; i<inputControls.length ; i++)
{
if ( inputControls[i].getAttribute("IsKeyField") == "Y" )
{
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C"))
{
if(inputControls[i].getAttribute("IsMandatory") == "Y")
{
if(inputControls[i].value == "")
{
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
}
}
}
}
Of course, you can call that code while clicking the required button.
btnUpload.Attributes.Add("onClick", "javascript:if(!ValidateMandatoryFields()) return false;");