Solved

Custom validator on dynamically added selects and textboxes

Posted on 2009-03-30
16
387 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for ur response...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
please help! urgent...your effort would be appreciated.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
Thanks alot!. do you know how to cal a custom validator client function code behind.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
the ids of the controls are the ones that you give it when you create the dynamic controls ..
0
 

Author Comment

by:pinky1307
Comment Utility
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
Comment Utility
>>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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

763 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

9 Experts available now in Live!

Get 1:1 Help Now