Custom validator on dynamically added selects and textboxes

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.
pinky1307Asked:
Who is Participating?
 
RejojohnyCommented:
>>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
 
Michel PlungjanIT ExpertCommented:
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
 
Michel PlungjanIT ExpertCommented:
OOps.

Can you post how the two sets of fields exist? I did not see you wanted EITHER filled
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Michel PlungjanIT ExpertCommented:
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
 
pinky1307Author Commented:
i want to know how i can get the id's of the dynamicaly added dropdownlists on to aspx page
0
 
pinky1307Author Commented:
Thanks for ur response...
0
 
Michel PlungjanIT ExpertCommented:
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
 
pinky1307Author Commented:
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
 
pinky1307Author Commented:
please help! urgent...your effort would be appreciated.
0
 
Michel PlungjanIT ExpertCommented:
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
 
pinky1307Author Commented:
Thanks alot!. do you know how to cal a custom validator client function code behind.
0
 
Michel PlungjanIT ExpertCommented:
Nope. Sorry. I am only javascripting

I will change the title and add the ASP.NET to your list of areas
0
 
RejojohnyCommented:
>>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
 
pinky1307Author Commented:
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
 
RejojohnyCommented:
the ids of the controls are the ones that you give it when you create the dynamic controls ..
0
 
pinky1307Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.