Solved

Code for Radio Button validation

Posted on 2012-03-28
9
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

 

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 NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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 …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

729 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