Solved

Code for Radio Button validation

Posted on 2012-03-28
9
312 Views
Last Modified: 2012-04-05
I need to add a check for Radio Button in my java script code for the form I am developing using Oracle portal 10g to make sure that one of the Radio Button is checked.I have code as per attached file for text field and fields which are selectable from the drop down menu.
 
What do I need to add or modify in my java script so that I can ensure Radio Button is checked.
Please let me know if more information is needed.
Thank you very much .
java-script.txt
0
Comment
Question by:lmymusic2000
  • 4
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37780449
Try this

Put it in an external file so you do not need to do all that messy HTP.p and HTP.print (why is there a difference?

<script type="text/javascript">
function IsSelected(s) {
  return s.options[s.selectedIndex].value != "";
}
function IsEmpty(s){  
  if (s.length != undefined) {
    for (i=0; i<s.length;i++){
      if (s[i].value !="") return true;
    }
    return false;
  }
  else return s.value != "";
}

function IsChecked(but) {
  if (!but.length) return but.checked;
  for (var i=0;i<but.length;i++) {
    if (but[i].checked) return true;
  }
  return false;
}

function validateForm(theForm) {
  if (IsEmpty(theForm.IN_LAST_NAME)) {
    alert("Please enter last name."); 
    theForm.IN_LAST_NAME.focus();
    return false;
  } 
  
  if (!IsSelected(theForm.IN_REQUESTED_CREDIT_HOURS)) {
    alert("Please select credit hours!"); 
    theForm.IN_REQUESTED_CREDIT_HOURS.focus();
    return false;
  } 

  if (!IsChecked(theForm.SOME_RADIO_OR_CHECKBUTTON_SET)) {
    alert("Please check a radio or check button!"); 
    return false;
  } 
  return true;
}
</script>

Open in new window


I have changed the code so you can do

<form onsubmit="return validateForm(this)"

instead of having the name of the form floating around the place

I also deleted an unused focus function and cleaned up the rest
0
 

Author Comment

by:lmymusic2000
ID: 37783299
Thank you mplungjan for the input.I already have coded for other fields so I did not change to external file.Besides I do not know how to use  external file for validation.I am creating this form through a oracle package I am writing.
I  tried as per attached file but now it shows the Alert message and even after clicking OK and selecting one of the Radio button it keeps giving me same Alert and does not go beyond that point.What could be wrong in the code?


Thank you very much.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37783673
I need to see the form

Can you update

http://jsfiddle.net/mplungjan/X4qpP/

with the html rendered (view-source) ?

I will look tomorrow
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.

 

Author Comment

by:lmymusic2000
ID: 37796160
Thank for your help.I was able to make it work.
It is working now with Radio button as well as other field validation also.
Thanks again.
0
 

Author Comment

by:lmymusic2000
ID: 37796284
I've requested that this question be closed as follows:

Accepted answer: 0 points for lmymusic2000's comment #37796160

for the following reason:

Based on the code provided by the expert I was able to modify it to suit my need and solve my issue.<br />Thank you.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 37796285
So I assume you wanted to give me some points?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37799367
In this case I have run out of t-shirts. Only glory left ;)
0
 

Author Closing Comment

by:lmymusic2000
ID: 37811848
Thank you for the solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

786 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