Solved

Custom validator on dynamically added selects and textboxes

Posted on 2009-03-30
16
391 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now