Jump to content

Need Help to modify JS script to validate Radio Button Selected


n1concepts

Recommended Posts

I found this JavaScript code that works flawlessly in ensuring an email (or at least something for that field) is defined.

 

However, I need to modify this code to ensure a "radio" button is selected and if not, similar error message is shown on the form web page & preventing the form from being submitted.

 

Here's the original code - which is unedited (I need it updated to validate the 'radio' button group' in the form:

 

// script.js handle the form submit event on form (below) entitled form.html
function prepareEventHandlers() {
document.getElementById("form3").onsubmit = function() {
	// I NEED THE BELOW LOGIC CHANGED TO VALIDATE RADIO BUTTON IS CHECKED.
	if (document.getElementById("func").value == "") {
		document.getElementById("errorMessage").innerHTML = "Please select option via radio button!";
		// to STOP the form from submitting
		return false;
	} else {
		// reset and allow the form to submit
		document.getElementById("errorMessage").innerHTML = "";
		return true;
	}
};
}

// when the document loads
window.onload =  function() {
prepareEventHandlers();
};

 

AND here is the actual form (form.html) I want the error message to appear if no radio button selected:

 

<form action="includes/validate.php" method="post" name="form3" id="form3">

                  <input type="radio" name="func" value="work" id="func_0" />
                  <input type="radio" name="func" value="nonwork" id="func_1" />

                   <span id="errorMessage"></span>     

                  <input type="submit" name="submit3" id="submit3" class="rcorners" value="next>" tabindex="15" /></td>
        </form>

        <script src="script.js"></script>

 

Note: the 'errorMessage' id is where Error message should put - which is the case if I was validating on email field.

Again, I need some help to modify the (above) JS script to check if one of the "func" radio buttons selected before allowing form to be submitted.

 

If neither button selected, then error message appear within SPAN tag id 'errorMessage'.

 

Thx for help!

Link to comment
Share on other sites

<!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" xml:lang="en" lang="en">
<head>
<title>New document</title>
<meta name="generator" content="TSW WebCoder 2010" />

<script type="text/javascript">
// script.js handle the form submit event on form (below) entitled form.html
function prepareEventHandlers() {
document.getElementById("form3").onsubmit = function() {
	if ((document.getElementById("func_0").checked == true) || (document.getElementById("func_1").checked == true)) {
		document.getElementById("errorMessage").innerHTML = "radio button is good to good";
		// to STOP the form from submitting
		//return true;    // uncomment this line to have submit working
		return false;    // remove line when using in the real world
	} else {
		// reset and allow the form to submit
		document.getElementById("errorMessage").innerHTML = "Please select one button!";
		return false;
	}
};
}

// when the document loads
window.onload =  function() {
prepareEventHandlers();
};
</script>

</head>

<body>

<form action="includes/validate.php" method="post" name="form3" id="form3">

                  <input type="radio" name="func" value="work" id="func_0" />
                  <input type="radio" name="func" value="nonwork" id="func_1" />

                   <span id="errorMessage"></span>

                  <input type="submit" name="submit3" id="submit3" class="rcorners" value="next>" tabindex="15" /></td>
        </form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.