Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign a validation control in form

Status
Not open for further replies.

lehuong

Programmer
Joined
Sep 2, 2003
Messages
98
Location
US
Hi all,
I have a form with 2 textfields: F1, F2 and 2 buttons: B1, B2. My question is that I want to have 2 field validations V1 for F1 and V2 for F2, when I click B1 it suppose only validate the F1 and click B2 it validate F2. The problem is that when I create these validations, any time I click B1 or B2 it validate both F1 and F1. Is there any way to create the validation control for a certain field and have the correspondent button?
Thanks
 
You are looking at conditional validations, and there is several ways how to do it. Here is a simple sample for your situation:
Code:
//in page code behind
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
 //
 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
 //
 InitializeComponent();
 base.OnInit(e);
 //attach javascript to each button
 if(Button1.Attributes["onclick"] == null)
 {
  Button1.Attributes.Add("onclick", "javascript:enableValidation('F1');");
 }

 if(Button2.Attributes["onclick"] == null)
 {
  Button2.Attributes.Add("onclick", "javascript:enableValidation('F2');");
 }
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{    
 this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

page html:
<HTML>
<HEAD>
 <script language=javascript>
  function enableValidation(controlName){
   if(controlName == &quot;F1&quot;){
    ValidatorEnable(vldF1, true);
    ValidatorEnable(vldF2, false);
   }
   else{
    ValidatorEnable(vldF2, true);
    ValidatorEnable(vldF1, false);
  }
 }
</script>
</HEAD>
<body>	
 <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
  <asp:TextBox id=&quot;F1&quot; runat=&quot;server&quot; />
 <asp:RequiredFieldValidator id=&quot;vldF1&quot; runat=&quot;server&quot; ControlToValidate=&quot;F1&quot; ErrorMessage=&quot;F1 invalid&quot; />
  <br>
  <asp:TextBox id=&quot;F2&quot; runat=&quot;server&quot; />
  <asp:RequiredFieldValidator id=&quot;vldF2&quot; runat=&quot;server&quot; ControlToValidate=&quot;F2&quot; ErrorMessage=&quot;F2 invalid&quot; />
  <br>		
  <asp:Button id=&quot;b1&quot; runat=&quot;server&quot; Text=&quot;Validate F1&quot; />
  <asp:Button id=&quot;b2&quot; runat=&quot;server&quot; Text=&quot;Validate F2&quot; />
 </form>	
</body>
</HTML>
Here is a URL to an MSDN article, explaining how to twitch validators, using client/server side APIs:Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top