Solved

javascript IF on radio button value

Posted on 2011-02-16
11
318 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Timerpicker in javascript 4 35
jquery wizard next doesn't go to top of tab 4 53
Basic JavaScript Question 3 38
Initialize SharePoint Online List Values in Quick Edit 7 21
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now