Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2006, 10:04 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
JavaScript validate form(option button)

I use this JavaScript to validate my form; the script works perfectly except when I tried making it work with option button, I tried sth like but it aint working
if (form.field_name.checked == false)

This is my js

<script language="JavaScript" type="text/javascript">
<!--
function checkform (form)
{
	
	if (form.field_name.value == "")
	{
		alert( "a msg error here");
		form.field_name.focus();
		return false ;
	}

	return true ;
}
//-->
</script>
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Apr 22nd, 2006, 11:14 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If you're referring to a radiobutton group, then remember that all buttons in a group have the same name. Each button is distinguished by its position in an array representing the group. In your example you would need to refer to a member thusly:
if (form.field_name [0].checked == false)
If you know which exact one you're checking, then you could use its ID.

Normally, I'm going to cycle through and check buttons in multiple forms, so I make an object variable for a group, run through the group, set the variable for another group, run through that group, etc., until I'm done.
The HTML stuff:
<input name="checklist" type="radio" value="catalog" 
	onClick="switchCart ('cat');" >
<input name="checklist" type="radio" value="cart" 
	defaultChecked checked onClick="switchCart ('cart');" >

The script stuff:
var cObj = document.forms ["headForm"].checklist;
if (cObj [0].checked) blah blah....
...
...
I recommend you develop a set of validation functions for names, addresses, email addresses, numeric amounts, etc. You'll find that you use them all the time. Don't forget to revalidate on the server, as well.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 22nd, 2006, 5:48 PM   #3
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
by specifying the position i will have to check all of them then!!, so this is what i did:
function checkform(form)
{
if ((form.same_room[0].checked == false) && (form.same_room[1].checked == false))
	{
		alert( "You Choose");
		form.same_room[0].focus();
		return false ;
	}
	return true ;
}

and this is the html part:
<td  class='style8'>
1) Do you mind if your exam is scheduled in the same room with 
another exam?<br /><br />
<input type='radio' name='same_room' value='yes' >Yes<br />
<input type='radio' name='same_room' value='no'>No<br /><br />
<i>If you chose YES, please give your reasons. If no reason is provided the request will be rejected:</i><br /><br />
<textarea rows='6' name='same_room_reason' cols='47'></textarea>
<br />
</td>

But stilll aint working!!
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Apr 22nd, 2006, 7:23 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
by specifying the position i will have to check all of them then!!
Excuse me? Who says?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 23rd, 2006, 5:14 AM   #5
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
thats what i understood.. what i meant is:
form.same_room[0].checked

would refer to the first option button, Right?
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Apr 23rd, 2006, 6:30 AM   #6
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
I got it working DaWei thanks for your tip

	else if ((document.getElementsByName('same_room')[0].checked==true) && (document.update.same_room_reason.value == ""))
	{
		alert( "Is your exam in the same room? if Yes then please give your reasons.");
		document.update.same_room_reason.focus();
		return false;
	}
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC