User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,615 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 704 | Replies: 6 | Solved
Reply
Join Date: Jun 2008
Posts: 8
Reputation: darkraider is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darkraider darkraider is offline Offline
Newbie Poster

Radio button

  #1  
Jun 26th, 2008
I try alot of way but still cant the radio button to work. i need the user to select one of them else an error msg will appear.

I got the error msg to appear but it still load over to the next page as if i selected the gender.
<html>
<head><title> Kelvin's Guestbook </title>
<script language="Javascript">

function validate_length(field)
{
   with(field)
   {
	if (value.length == 0)
	{
		alert("Pls Enter Your Name!"); return false;
	}else return true;

   }
}
function validate_email(field)
{
   with (field)
   {
	apos=value.indexOf("@");	
	dotpos=value.lastIndexOf(".");	
	if (apos<1||dotpos-apos<2)
	{
		alert("Not a valid e-mail address!"); return false;
	}else return true;
   }
}
function validate_gender(field)
{
   with (field)
   {
	var male = document.getElementById("male");
	var female = document.getElementById("female");
	if (male.checked)
	{
		
	}	
	if (female.checked)
	{
		
	}
	else
	{
		alert("Pls Select Your Gender");return false;
	}
   }
}


function validate_form(thisform)
{
   with (thisform)
   {
	if (validate_length(yourname) == false)
	{
		yourname.focus(); return false;
	}
	if (validate_email(email)==false || validate_email(email)==false)
	{
		email.focus(); return false;
	}
	if (validate_gender(gender) == false)
	{
		gender.focus(); return false;
	}
   }
}
function startclock()
{
var thetime=new Date();

var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
var AorP=" ";

if (nhours>=12)
    AorP="P.M.";
else
    AorP="A.M.";

if (nhours>=13)
    nhours-=12;

if (nhours==0)
   nhours=12;

if (nsecn<10)
 nsecn="0"+nsecn;

if (nmins<10)
 nmins="0"+nmins;

if (nday==0)
  nday="Sunday";
if (nday==1)
  nday="Monday";
if (nday==2)
  nday="Tuesday";
if (nday==3)
  nday="Wednesday";
if (nday==4)
  nday="Thursday";
if (nday==5)
  nday="Friday";
if (nday==6)
  nday="Saturday";

nmonth+=1;

if (nyear<=99)
  nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
 nyear+=1900;

document.clockform.clockspot.value
=nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nmonth+"/"+ntoday+"/"+nyear+","+nday;


setTimeout('startclock()',1000);

} 

</script>
</head>
<body>

<FORM name="clockform">
Current Time: <INPUT TYPE="text" name="clockspot" size="32">
</FORM>
<SCRIPT language="JavaScript">
startclock();
</SCRIPT>

<Center>

<form name="guestbook" action="1.html" 
 onsubmit="return validate_form(this);" method="post">

	 <br>Name : <input type="text" name="yourname" value="">
	 <br>Email: <input type="text" name="email" size="30">
	 <br>Gender: <input type="radio" name="gender" value="Male" id="Male">Male
		  <input type="radio" name="gender" value="Female" id="Female">Female
	 <br>Comments: *Optional
	 <br><textarea cols="50" rows="8" name="Coments"></textarea>
	 <br><input type="submit" name="submit" value="Sign GuestBook">
	     <input type="Reset" name="Reset" value="Reset">
</form>
</Center>
</body>
</html>
Pls reply asap for a solution, need to complete it by next week. and can only start designing the background n stuff after all the field are correct thk.
Last edited by Narue : Jun 26th, 2008 at 5:40 pm. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 8
Reputation: darkraider is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darkraider darkraider is offline Offline
Newbie Poster

Re: Radio button

  #2  
Jun 26th, 2008
only can use javascript nothing else. thk again
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Radio button

  #3  
Jun 26th, 2008
You know what makes ur life hell here.

Yes your Code itself. What the hell u have coded. No code tags.
As u r a new poster, I am solving ur problem.

Here is ur code that i modified (is this was ur code!!)

  1. <html>
  2. <head><title> Kelvin's Guestbook </title>
  3. <script language="Javascript" type="text/javascript">
  4.  
  5. function validate_length(field)
  6. {
  7. with(field)
  8. {
  9. if (value.length == 0)
  10. {
  11. alert("Pls Enter Your Name!"); return false;
  12. }else return true;
  13.  
  14. }
  15. }
  16.  
  17.  
  18. function validate_email(field)
  19. {
  20. with (field)
  21. {
  22. apos=value.indexOf("@");
  23. dotpos=value.lastIndexOf(".");
  24. if (apos<1||dotpos-apos<2)
  25. {
  26. alert("Not a valid e-mail address!"); return false;
  27. }else return true;
  28. }
  29. }
  30.  
  31.  
  32. function validate_gender(gM, gF)
  33. {
  34.  
  35. //alert("In");
  36. //var male = document.getElementById("Male");
  37. //var female = document.getElementById("Female");
  38. if (gM.checked)
  39. {
  40. return true;
  41. }
  42. if (gF.checked)
  43. {
  44. return true;
  45. }
  46.  
  47. alert("Pls Select Your Gender");
  48. return false;
  49.  
  50.  
  51. }
  52.  
  53. /**/
  54. function validate_form(thisform)
  55. {
  56. with (thisform)
  57. {
  58. if (validate_length(yourname) == false)
  59. {
  60. yourname.focus(); return false;
  61. }
  62. if (validate_email(email)==false || validate_email(email)==false)
  63. {
  64. email.focus(); return false;
  65. }
  66. if (validate_gender(genderM, genderF) == false)
  67. {
  68. genderM.focus(); return false;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. function startclock()
  75. {
  76. var thetime=new Date();
  77.  
  78. var nhours=thetime.getHours();
  79. var nmins=thetime.getMinutes();
  80. var nsecn=thetime.getSeconds();
  81. var nday=thetime.getDay();
  82. var nmonth=thetime.getMonth();
  83. var ntoday=thetime.getDate();
  84. var nyear=thetime.getYear();
  85. var AorP=" ";
  86.  
  87. if (nhours>=12)
  88. AorP="P.M.";
  89. else
  90. AorP="A.M.";
  91.  
  92. if (nhours>=13)
  93. nhours-=12;
  94.  
  95. if (nhours==0)
  96. nhours=12;
  97.  
  98. if (nsecn<10)
  99. nsecn="0"+nsecn;
  100.  
  101. if (nmins<10)
  102. nmins="0"+nmins;
  103.  
  104. if (nday==0)
  105. nday="Sunday";
  106. if (nday==1)
  107. nday="Monday";
  108. if (nday==2)
  109. nday="Tuesday";
  110. if (nday==3)
  111. nday="Wednesday";
  112. if (nday==4)
  113. nday="Thursday";
  114. if (nday==5)
  115. nday="Friday";
  116. if (nday==6)
  117. nday="Saturday";
  118.  
  119. nmonth+=1;
  120.  
  121. if (nyear<=99)
  122. nyear= "19"+nyear;
  123.  
  124. if ((nyear>99) && (nyear<2000))
  125. nyear+=1900;
  126.  
  127. document.clockform.clockspot.value=nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nmonth+"/"+ntoday+"/"+nyear+","+nday;
  128.  
  129.  
  130. setTimeout('startclock()',1000);
  131.  
  132. }
  133.  
  134. </script>
  135. </head>
  136. <body>
  137.  
  138. <FORM name="clockform">
  139. Current Time: <INPUT TYPE="text" name="clockspot" size="32">
  140. </FORM>
  141. <SCRIPT language="JavaScript">
  142. startclock();
  143. </SCRIPT>
  144.  
  145. <Center>
  146.  
  147. <form name="guestbook" action="1.html"onsubmit="return validate_form(this);" method="post">
  148.  
  149. <br>Name : <input type="text" name="yourname" value="">
  150. <br>Email: <input type="text" name="email" size="30">
  151. <br>Gender: <input type="radio" name="genderM" value="Male" id="Male">Male
  152. <input type="radio" name="genderF" value="Female" id="Female">Female
  153. <br>Comments: *Optional
  154. <br><textarea cols="50" rows="8" name="Coments"></textarea>
  155. <br><input type="submit" name="submit" value="Sign GuestBook">
  156. <input type="Reset" name="Reset" value="Reset">
  157. </form>
  158. </Center>
  159. </body>
  160. </html>

Now figure out what was wrong in ur code.
Last edited by Luckychap : Jun 26th, 2008 at 4:17 pm.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jun 2008
Posts: 8
Reputation: darkraider is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darkraider darkraider is offline Offline
Newbie Poster

Re: Radio button

  #4  
Jun 26th, 2008
Ya very new to this lol, thk for ur help.

Only got 2 lesson on it and the teacher want us to create something like this LOL.
Reply With Quote  
Join Date: Jun 2008
Posts: 8
Reputation: darkraider is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darkraider darkraider is offline Offline
Newbie Poster

Re: Radio button

  #5  
Jun 26th, 2008
Sry to trouble the ppl here again but i need it to be able to select either male or female.

What luckychap did work but it can select both.
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Radio button

  #6  
Jun 27th, 2008
hey just want everythin to done here!!

Show some effort dude.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jun 2008
Posts: 8
Reputation: darkraider is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darkraider darkraider is offline Offline
Newbie Poster

Re: Radio button

  #7  
Jun 27th, 2008
Still cant get the radio button to work, if i nv select it giv me the msg but still load to the page as if i select a choice. Can help see where i went wrong ??
<html>
<head><title> Guestbook </title>
<style>
H1, H2, H3, H4, H5, H6 {color:#D4A017; text-align:center}
</style>
<script language="Javascript" type="text/javascript">

	function validate_length(field)
	{
	with(field)
	{
	        if (value.length == 0)
	       {
	                alert("Pls Enter Your Name!"); return false;
	        }else return true;
	 
	}
	}
	 
	 
	function validate_email(field)
	{
	with (field)
	{
	         apos=value.indexOf("@");
	         dotpos=value.lastIndexOf(".");
	         if (apos<1||dotpos-apos<2)
	        {
	                  alert("Not a valid e-mail address!"); return false;
	        }else return true;
	}
	}
	 
	function validate_gender(gender) 
	{
		var genderChecked 
		for (var i=0; i<document.guestbk.gender.length; i++) 
		{
		if (document.guestbk.gender[i].checked) 
		{
		genderChecked = document.guestbk.gender[i].value
		}
		
	}
		if(!genderChecked)
		{ 
			alert("Pls Select Your Gender")
	}else return true;
	
	}
	

	function validate_form(thisform)
	{
	with (thisform)
	{
		if (validate_length(yourname) == false)
		{
			yourname.focus(); return false;
		}
		if (validate_email(email) == false)
		{
			email.focus(); return false;
		}
		if (validate_gender(gender) == false)
		{
			gender.focus(); return false;
		}
				
	}
	} 
</script>
</head>
<body>

<h1>Welcome to my GuestBook</h1>

<Center>
 
<form name="guestbk" action="1.html" onsubmit="return validate_form(this);" 
method="post">
	 
<br>Name : <input type="text" name="yourname" value="">
<br>Email: <input type="text" name="email" size="30">
<br>Gender: <input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<br>My Website Rating: 
<select name="Number">
<option value="0"SELECTED>SELECT</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br>Comments: *Optional
<br><textarea cols="50" rows="8" name="Coments"></textarea>
<br><input type="submit" name="submit" value="Sign GuestBook">
<input type="Reset" name="Reset" value="Reset">
</form>
</Center>
</body>
</html>

thk's.
Last edited by ~s.o.s~ : Jun 28th, 2008 at 2:51 am. Reason: Added code tags, learn to use them.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 12:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC