Solved

Code for Radio Button validation

Posted on 2012-03-28
9
309 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:lmymusic2000
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:lmymusic2000
Comment Utility
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
Comment Utility
So I assume you wanted to give me some points?
0
 
LVL 75

Expert Comment

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

Author Closing Comment

by:lmymusic2000
Comment Utility
Thank you for the solution.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

12 Experts available now in Live!

Get 1:1 Help Now