By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

problem with validating input value

P: n/a
Hi

I've got html form and i want to validate it using javascript

I have somerthing like this:

<p>
<b>Gender: <font color="red">*</font></b>
<input type="radio" value="Male" name="gender">Male
<input type="radio" value="Female" name="gender">Female
</p>

how can i check if any of two was chosen?

I tried something like this?:
if(document.dane.gender.value=="")

dane is a form name
gender is a input name

For now im getting error message saying that document.dane.gender.value is
not an object

Thanks.
Leszek

Jan 23 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Leszek wrote:
Hi

I've got html form and i want to validate it using javascript

I have somerthing like this:

<p>
<b>Gender: <font color="red">*</font></b>
The font element is deprecated, use a span with inline style or class.
Also, gender is generally in regard to language, sex is in regard to
biology (but that may spark a long debate!):

<b>Sex: <span style="color: red;">*</span></b>

<input type="radio" value="Male" name="gender">Male
<input type="radio" value="Female" name="gender">Female
With radio buttons, one should be selected by default however I don't
think any browsers actually enforce it. The issue being that once one
is selected, you can't get back to none selected.

</p>

how can i check if any of two was chosen?

I tried something like this?:
if(document.dane.gender.value=="")

dane is a form name
gender is a input name

For now im getting error message saying that document.dane.gender.value is
not an object


Use the onsubmit attribute of the form element:

<form name="dane" onsubmit="return validateForm(this);" ... >
Then your validateForm() function can be something like:

function validateForm(form)
{
var el = form.dane;
for (var i=0, len=el.length; i<len; ++i){
if (el[i].checked) return true;
}
alert('You must indicate your sex');
return false;
}
You may want to add validation routines for other form controls.
--
Rob
Jan 23 '06 #2

P: n/a
>>how can i check if any of two was chosen?

If you add attribute "checked" to one of this radio buttons you achieve
required result without any script.

<p>
<b>Gender: <font color="red">*</font></b>
<input type="radio" value="Male" name="gender" checked>Male
<input type="radio" value="Female" name="gender">Female
</p>

Sorry for my English

Jan 23 '06 #3

P: n/a
marss wrote:

If you add attribute "checked" to one of this radio buttons you achieve
required result without any script.


Yes, that is correct but it's a constraint that doesn't fit with how
most would like to use radio buttons. In most forms it is a requirement
of the designer that neither be selected by default and that only one
can be selected.

So you are caught between using radio buttons with none selected by
default and a script to ensure one is selected, or checkboxes with
script to ensure only one is selected.

Damned if you do, damned if you don't.
--
Rob
Jan 24 '06 #4

P: n/a
On 2006-01-23, RobG <rg***@iinet.net.au> wrote:
marss wrote:

If you add attribute "checked" to one of this radio buttons you achieve
required result without any script.


Yes, that is correct but it's a constraint that doesn't fit with how
most would like to use radio buttons. In most forms it is a requirement
of the designer that neither be selected by default and that only one
can be selected.

So you are caught between using radio buttons with none selected by
default and a script to ensure one is selected, or checkboxes with
script to ensure only one is selected.

Damned if you do, damned if you don't.


add an extra radio button with style="display:none" and make that checked.

--

Bye.
Jasen
Jan 25 '06 #5

P: n/a
Jasen Betts <ja***@free.net.nz> wrote:
On 2006-01-23, RobG <rg***@iinet.net.au> wrote:
marss wrote:

If you add attribute "checked" to one of this radio buttons you achieve
required result without any script.


Yes, that is correct but it's a constraint that doesn't fit with how
most would like to use radio buttons. In most forms it is a requirement
of the designer that neither be selected by default and that only one
can be selected.

So you are caught between using radio buttons with none selected by
default and a script to ensure one is selected, or checkboxes with
script to ensure only one is selected.

Damned if you do, damned if you don't.


add an extra radio button with style="display:none" and make that checked.


Didn't work for me. The button wanted to be visible to be part of the
button array.

--
Ed Jay (remove M to respond by email)
Jan 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.