Solved

Custom validator on dynamically added selects and textboxes

Posted on 2009-03-30
16
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 250 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

623 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