I’m having a problem with some code, here’s the code below:
<script language=”JavaScript”>
function ValidateForm(form){
ErrorText= “”;
if ( ( form.Consent[0].checked == false ) && ( form.Consent[1].checked == false ) ) { alert ( “Please choose your Yes or NO for our Electronic Signature” ); return false; }
if (ErrorText= “”) { form.submit() }
}
</script>
[code=html] <form action=”congratulations_aff.php” method=”post” name=”congratulations_aff”> <input type=”submit” value=”submit” value=”Submit” onClick=”ValidateForm(this.form)” /><input type=”reset” value=”Reset”>
If someone hits submit without selecting yes or no the validation pops up asking them to make the selection. After they close out the pop up box the form action still passes them along to the congratulations_aff.php page.
How and why is that passing them on even if they didn’t make a selection with the radio buttons?
[code=html]<script>
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
return fnd; // return option index of selection
// comment out next line if option index used in line above
// return str;
}
function checkForm() {
var chosen = getRBtnName('test');
if (chosen < 0) {
alert( "Please choose one answer when you are asked to select a number." );
return false;
} else { return true; }
// Note: Could also choose to return selection, if needed
}
</script>[/code]
[code=html]function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var b=0; b<sel.length; b++) {
if (sel[b].checked == true) { str = sel[b].value; fnd = b; }
}
return fnd; // return option index of selection
// comment out next line if option index used in line above
// return str;
}
function checkForm() {
var chosen = getRBtnName('USPersonYesNo');
if (chosen < 0) {
alert( "For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?" );
return false;
} else { return true; }
// Note: Could also choose to return selection, if needed
}
[/code]
[CODE]
var sel = document.getElementsByName(GrpName);[/CODE]
[CODE]
for (var b=0; b<sel.length; b++) {
if (sel[b].checked == true) { str = sel[b].value; fnd = b; }
}
[/CODE]
[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Radio Check</title>
<script>
<!--
function checkChecked( btnGrpName ) {
var radioSet = document.getElementsByName( btnGrpName ); // grab the radio group
for (var btn=0; btn<radioSet.length; btn++)
if ( radioSet[btn].checked == true)
return true; // We return true on the first instance of a radio button checked
return false; // return false as we didn't find one
}
function processForm(){
var ok = checkChecked( 'USPersonYesNo' ); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
}
return ok; // we are returning either a true or false..
}
//-->
</script>
</head>
<body><form action="#" name="radiocheck" id="radiocheck" onsubmit="return processForm();">
<table>
<tr>
<td colspan="3" valign="top"><p>
<input type="radio" name="USPersonYesNo" value="Y" />
Yes<br />
<br />
<input type="radio" name="USPersonYesNo" value="N" />
No</p>
<input name="submit" value="Submit" type="submit" />
</td></tr></table></form>
</body>
</html>[/code]
<script>
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
return fnd; <br/>
}
function checkForm() {
var chosen = getRBtnName('Consent');
if (chosen < 0) {
alert( "Please choose one answer when you are asked to select a number." );
return false;
} else { return true; }
}
</script>
<form action="congratulations_aff.php" method="post" name="congratulations_aff" onSubmit="return checkForm()">
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<td colspan="3">I consent to providing my electronic signature.</p></td>
</tr>
<tr>
<td colspan="3" valign="top">
<input type="radio" name="Consent" value="Y" />
Yes
<i> </i> <input type="radio" name="Consent" value="N" />
<i> </i> No
<i> </i> <table border="0" cellspacing="1" cellpadding="0">
<i> </i> <tr>
<i> </i> <td>I consent to electronic receipt of my information reporting documentation.</td>
<i> </i> </tr>
<i> </i> <tr>
<i> </i> <td valign="top">
<i> </i> <input type="radio" name="Consent1099YesNo" value="Y" />
<i> </i> Yes
<i> </i> <input type="radio" name="Consent1099YesNo" value="N" />
<i> </i> No</td>
<i> </i> </tr>
<i> </i> <tr>
<i> </i> <td valign="top">
<i> </i> For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?
<i> </i> <input type="radio" name="USPersonYesNo" value="Y" />
<i> </i> Yes
<i> </i> <input type="radio" name="USPersonYesNo" value="N" />
<i> </i> No</tr>
<i> </i> </table>
<input type="submit" value="submit" value="Submit" />
</form>
<i>
</i>
function processForm(){
var ok = checkChecked( 'USPersonYesNo' ); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
}
return ok; // we are returning either a true or false..
}
[CODE]var ok = checkChecked( 'USPersonYesNo'); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
return ok;
}
var ok = checkChecked( 'otherUSPersonYesNo'); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
return ok;
}[/CODE]
[CODE]
if (!checkChecked( 'USPersonYesNo')){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
return false;
}
if (!checkChecked( 'otherUSPersonYesNo')){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
return false;
}
return true; // get here and were doing OK
[/CODE]
<script>
function checkChecked( btnGrpName ) {
var radioSet = document.getElementsByName( btnGrpName ); // grab the radio group
for (var btn=0; btn<radioSet.length; btn++)
if ( radioSet[btn].checked == true)
return true; // We return true on the first instance of a radio button checked
return false; // return false as we didn't find one
}
function processForm(){
var ok = checkChecked( 'USPersonYesNo' ); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
}
return ok; // we are returning either a true or false..
}
{
var ok = checkChecked( 'Consent' ); // returns true or false
if (!ok){
alert("For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
}
return ok; // we are returning either a true or false..
}
</script>
[CODE]function checkChecked( btnGrpName , msg ) {
var radioSet = document.getElementsByName( btnGrpName ); // grab the radio group
for (var btn=0; btn<radioSet.length; btn++)
if ( radioSet[btn].checked == true)
return true; // We return true on the first instance of a radio button checked
alert( msg );
return false; // return false as we didn't find one
}
function processForm(){
var ok = checkChecked( "USPersonYesNo" ,"For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
if (!ok) return ok
var ok = checkChecked( "otherUSPersonYesNo" ,"For tax purposes are you a NON U.S. citizen, NON U.S. resident, NON U.S. partnership, or NON U.S. corporation?");
if (!ok) return ok
return ok; // we are returning either a true or false..
}[/CODE]
you pass a second parameter, the warning message you want displayed on failure. Stacking the elements that need checking for radio inputs, with modification you could expand the checking scope to include other grouped elements.It's not working properly, it's loading after the button is being submitted.[/QUOTE]
<i>
</i><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<link rel="stylesheet" type="text/css" href="ew/style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function checkChecked( btnGrpName , msg ) {
var radioSet = document.getElementsByName( btnGrpName ); // grab the radio group
for (var btn=0; btn<radioSet.length; btn++)
if ( radioSet[btn].checked == true)
return true; // We return true on the first instance of a radio button checked
alert( msg );
return false; // return false as we didn't find one
}
function processForm(){
var ok = checkChecked( "USPersonYesNo" ,"For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?");
if (!ok) return ok
var ok = checkChecked( "otherUSPersonYesNo" ,"For tax purposes are you a NON U.S. citizen, NON U.S. resident, NON U.S. partnership, or NON U.S. corporation?");
if (!ok) return ok
<i> </i>return ok; // we are returning either a true or false..
}
</script>
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="css/ie7.css">
<![endif]-->
</head>
<body>
<form action="congratulations_aff.php" method="post" name="congratulations_aff">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><p align="center"><h1>Consent for Electronic Signature</h1></p></td>
<i> </i><td colspan="3" valign="top"><p>
<i> </i> <input type="radio" name="Consent" value="Y" />
<i> </i> Yes<br />
<i> </i> <br />
<i> </i> <input type="radio" name="Consent" value="N" />
<i> </i> No</p>
<i> </i> <td><p align="center"><h1>Consent for Electronic 1099 Form</h1></p></td>
<i> </i> <td valign="top"><p>
<i> </i> <input type="radio" name="Consent1099YesNo" value="Y" />
<i> </i> Yes<br />
<i> </i> <br />
<i> </i> <input type="radio" name="Consent1099YesNo" value="N" />
<i> </i> No</p></td>
<i> </i> For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?
<i> </i> <input type="radio" name="USPersonYesNo" value="Y" />
<i> </i> Yes<br />
<i> </i> <br />
<i> </i> <input type="radio" name="USPersonYesNo" value="N" />
<i> </i> No<br />
<i> </i> </p></td>
<i> </i> </tr>
<i> </i> </table>
<input type="submit" value="submit" value="Submit" onSubmit="return checkForm()" />
</form> <br/>
</body>
</html>
function getRBGroupValue( RBGroup )
{
if ( RBGroup.length == null ) { return RBGroup.checked ? RBGroup.value : null; }
for (var i=0; i < RBGroup.length; i++ )
{
if ( RBGroup[i].checked ) return RBGroup[i].value;
}
return null;
}
function checkForm(form)
{
var chosen = getRBGroupValue( form.Consent );
if (chosen == null) {
alert( "Consent for Electronic Signature" );
return false;
}
chosen = getRBGroupValue( form.Consent1099YesNo );
if ( chosen == null ) {
alert( "Consent for Electronic 1099 Form." );
return false;
}
chosen = getRBGroupValue( form.USPersonYesNo );
if ( chosen == null ) {
alert( "For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation?" );
return false;
}
chosen = getRBGroupValue( form.USPersonYesNo );
if ( chosen == null ) {
alert( "Individual or Business?" );
return false;
}
<i> </i>// other form validation goes here
<i> </i>return true; // only do this if everything validates
}
</script>
And that because your not observing typos, name conventions, the function I posted worked fine, you neglected to rename the primary function to processForm from checkForm.
It would serve you well if you were to check for errors you introduce in to your code when dealing with programming problems that you seek help with, it is very common for people to come here with an issue and they have cobbled together a pseudo type of code, it is fixed or altered to work by the person answering the query to then be told the code does not work because the person copies and pastes and expects things to just go and as soon as an error occurs the knee jerk response is that "Your code does not work"
So from a development aspect, most of the errors people encounter are because they are expecting a fully working script when we generally will advise, point in the right direction and give examples that do similar or return the expected result but will require some input from the person having an issue in figuring out the missing bits for themselves, thats the idea of giving someone a leg up on the scripting ladder.
Personally I prefer to give examples or point people in the right direction and have people "Learn" how to do it rather than complain that something does not work when it very well does...[/QUOTE]
0.1.9 — BETA 6.17