Solved

javascript IF on radio button value

Posted on 2011-02-16
11
319 Views
Last Modified: 2012-05-11
I have a radio group, bill 3 radio buttons
values:
a
b
c

I could ge the radio button value.

On the c value i want to do a javascript if() to validate the credit card fields only if radio button with the c value is selected
New-Text-Document--6-.txt
0
Comment
Question by:smares32371
  • 5
  • 5
11 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34911988
Check this :


<!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=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function validate() {
		var tp = document.getElementsByName("typep");
		var checkIsSelected = tp[0].checked;
		var paypalIsSelected = tp[1].checked;
		var creditCardIsSelected = tp[2].checked;
		var creditCardNumberIsValid = validateCrediCardNumber(document.getElementById("cnumber"));
		var creditCardIsNotOK = !creditCardIsSelected || !creditCardNumberIsValid;
		if( creditCardIsSelected && creditCardIsNotOK ) {
			alert("Please enter a valid credit card number")
		}
		return checkIsSelected || paypalIsSelected || !creditCardIsNotOK; 
	}
	function validateCrediCardNumber(field) {
		// simple check here
		return field.value.length>0;
	}
</script>
</head>
<body>
<form onsubmit="return validate();" action="#">
    <input type="radio" name="typep" value="a" />Check<br />
    <input type="radio" name="typep" value="b" />Paypal<br />
    <input type="radio" name="typep" value="c" />Credit card&nbsp;<input type="textbox" name="cnumber" id="cnumber" /><br />
    <input type="submit" value="Submit" />
</form>
</body>
</html>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34914879
@smares32371: not sure if you need this advice, but i would suggest that you go for a drop-down rather than radio buttons for this one. In this way, all you need to do is, check the value of one field.

Let me know if you code base for this.
Please no points for this comment of mine, since if @leak's reply works, he deserve all points :)
0
 

Author Comment

by:smares32371
ID: 34917886
leakim971: ok its working how do i validate the rest of the credit card fields, there is like 10 more
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 82

Expert Comment

by:leakim971
ID: 34917949
0
 

Author Comment

by:smares32371
ID: 34918114
I just want to make sure that the credit card fields are filled in .
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34918416
line 21 :
return field.value.length>0;

Open in new window


we check the field have one char (>0)
if you want 10 charactere or more :
return field.value.length>=10;

Open in new window

0
 

Author Comment

by:smares32371
ID: 34918529
I meant I want 2 other fields to give alerts if they not filled in
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34918870
close the question, award points (or not), try to understand the code, create a new question if you encounter difficulties
0
 

Author Comment

by:smares32371
ID: 34918946
i do understand the code but it only allows you to validate one field if the third radio button is selected lets forget about credit card fields, in general how would i validate normal fields base on the third button when there is more than one field.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34919278
what do you mean by validate ? Not empty ? Check the length propertie of its value (a string)

var myField_Value = document.getElementById( myField_ID ).value;
var myField_Is_Filled = myField_Value.length > 0; // this return true or false

with a field like this one :

<input type="text" id="myField_ID" name="myField_Name" />

Open in new window


to validate multiple fields : return myField1_Is_Filled && myField2_Is_Filled && myField3_Is_Filled;
0
 

Author Comment

by:smares32371
ID: 34919727
yes not empty
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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