Is it possible to have a required field validator for Taxonomy webtagging control. I tried using standard Required field validator but it fails. Is there any property in taxonomy webtagging control.??
4 Answers
var value = $("#<%=TaxonamyControlID.ClientID %>").find("input[type='hidden']").val()
if(value == '')
{
alert('please enter a value') ; return false;
}
else
{
return true;
}
I think Required Field Validator that comes out of the box will not work with Taxonomy web tagging control because the tagging control which appears like a text box (input type=text) is NOT an input control when it renders in browser.
You can use CustomValidator though as shown in article below:
http://asp.net-tutorials.com/validation/custom-validator/
At server side you can check Text Property of control and if its empty, set e.IsValid = false.
Actual value of the taxonomy input field is stored in a hidden input field.
If Javascript is OK for you, and you can surround your taxonomy control with DIV ID myTaxDiv, you can get the value quite easily. I've used this jQuery command:
j("#myTaxDiv > span > span > input[type='hidden']").val("");
I've been able to successfully validate a TaxonomyWebTaggingControl using a CustomValidator as shown below. In my case the taxonomy field is not required but I wanted to make sure the user entered a valid value. In your case you could update the event handler to check if the user entered a value and set the ErrorMessage appropriately.
Be sure NOT to set the ControlToValidate property of the CustomValidator or you will get an error.
In your ASPX page
<Taxonomy:TaxonomyWebTaggingControl ID="region" runat="server"></Taxonomy:TaxonomyWebTaggingControl><br />
<asp:CustomValidator ID="regionValidator" runat="server" Display="Dynamic" OnServerValidate="regionValidator_ServerValidate"></asp:CustomValidator>
In your code behind
protected void regionValidator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string reason;
if (!region.Validate(out reason))
{
args.IsValid = false;
regionValidator.ErrorMessage = reason;
}
}