Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Custom validator on dynamically added selects and textboxes

Posted on 2009-03-30
16
Medium Priority
?
415 Views
Last Modified: 2012-05-06
i am adding drop downlists and textboxes dynamically in a function. The id's of ddl's are generated from a datatable. i need to validate using javascript like if dropdownlist is selected and textbox has some text it should show a message that both cannot be added. Iam using custom validator but not undertsnading how to get the control ids of the element and validate it.
Any help please.
0
Comment
Question by:pinky1307
  • 7
  • 6
  • 3
16 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24026611
In plain javascript I would do


function validate(theForm) {
  for (var i=0, n=theForm.elements.length;i<n;i++) {
    var elem = theForm.elements[i];
    if (elem.type=="text" && elem.value=="") {
      alert('Please enter a value');
      elem.focus();
      return false
    }
    if (elem.type && elem.type.indexOf("select")!=-1 && elem.selectedIndex==0) {
      alert('Please select a value');
      elem.focus();
      return false
    }
  }
  return true; // allow submit
}
 
 
and use
<form onSubmit="return validate(this)">

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24026616
OOps.

Can you post how the two sets of fields exist? I did not see you wanted EITHER filled
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24026649
If they are next to each other try

function validate(theForm) {
  for (var i=0, n=theForm.elements.length;i<n;i++) {
    var elem = theForm.elements[i];
    if (elem.type && elem.type.indexOf("select")!=-1 && elem.selectedIndex!=0) {
      if (theForm.elements[i+1].value!='') {
        alert('Please EITHER select a value OR enter a value in the field');
        elem.focus();
        return false
      }
    }
  }
  return true; // allow submit
}

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:pinky1307
ID: 24028992
i want to know how i can get the id's of the dynamicaly added dropdownlists on to aspx page
0
 

Author Comment

by:pinky1307
ID: 24028998
Thanks for ur response...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24029101
Like this?



function getSelects(theForm) {
  var selects = new Array()
  for (var i=0, n=theForm.elements.length;i<n;i++) {
    var elem = theForm.elements[i];
    if (elem.type && elem.type.indexOf("select")!=-1) {
      selects[selects.length]= elem.id;
    }
  }
  return selects;
}
 
alert(getSelects(document.forms[0]))

Open in new window

0
 

Author Comment

by:pinky1307
ID: 24029186
its not a form. its a table.and the controls are added codebehind. so i need to add a custom validator for these dynamically added dropdownlists with client side function using javascript.
0
 

Author Comment

by:pinky1307
ID: 24029417
please help! urgent...your effort would be appreciated.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24029477
All questions are Urgent.


function getSelects() {
  var ids = new Array()
  var selects = document.getElementsByTagName('select');
  for (var i=0, n=selects.length;i<n;i++) {
      ids[ids.length]= selects[i].id;
    }
  }
  return ids;
}
 
alert(getSelects())

Open in new window

0
 

Author Comment

by:pinky1307
ID: 24029538
Thanks alot!. do you know how to cal a custom validator client function code behind.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24029568
Nope. Sorry. I am only javascripting

I will change the title and add the ASP.NET to your list of areas
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 24059640
>>i need to validate using javascript like if dropdownlist is selected and textbox has some text it should show a message that both cannot be added. Iam using custom validator but not undertsnading how to get the control ids of the element and validate it.

if the dropdown is dynamically added, you can also add a requirefieldvalidator and set the controltovalidate property to this dropdown. Also rememeber to set the initialValue attribute of the validator control.. you do not need a custom validator ..

but if you really need to add a custom validator, you can add it just the way you added the dropdown dynamically .. and javascript to be called can be set in the "clientvalidationfunction" property of the validator control .. you have to option on how to add the javascript. Add it dynamically again by using page.clientscript.registerclientscriptblock or just add it to your aspx page within the <head> tags .. I think adding it to the aspx page would be easier ..

Rejo
0
 

Author Comment

by:pinky1307
ID: 24059934
Iam sorry, i think u have not understood the question. How can i get the id's of the dynamically generated controls code behind. The controls have dufferent names which come from a datatable. i need to write a javascript function in aspx page and get the id's of the controls generated dynamically from code behind,
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 24059956
the ids of the controls are the ones that you give it when you create the dynamic controls ..
0
 

Author Comment

by:pinky1307
ID: 24060158
yeah but how to pass them on to aspx page. if we do document.getelement by id, what id do i need to give?
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 1000 total points
ID: 24063226
>>if we do document.getelement by id, what id do i need to give?
why do you need to pass it on to the aspx page? do you need it within the customvalidator funciton? if yes, you could use the parameters of the function to get access to the controls

Anyway, if you have other dynamic controls to be accessed in javascript, you could use
document.getElementById('<%=ControlId.ClientId%>');
within the javscript code ... where ControlId is the id that you gave to your dynamic control

Rejo
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question