Solved

Code for Radio Button validation

Posted on 2012-03-28
9
314 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

730 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