473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

problem with validating input value

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
5 1435
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
>>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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bartosz Wegrzyn | last post by:
Please look at my code. I do validation of my form before I submit it. I want to be able to to press one form button without running validating script. I just want to go directly to my php...
3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
1
by: AnnMarie | last post by:
Can someone let me know why my phone validation doesn't work within my validate function. It works alone, but not within this function like the other validations do. Phone is not required, but...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
8
by: Shane Jones | last post by:
on my php page i have a submit button and when I submit i want to validate that dome fields have data in. i have this as my submit button <input name="Enter" type="button"...
6
by: Advo | last post by:
Hi there. Having a few issues and cannot see a way to actually do the following. ive duplicated a simple questionnaire as shown here: http://www.hackeradio.com/questionnaire.html meaning...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
2
by: SONIQ | last post by:
Using javascripts to validate this form. Basic operation, when a user clicks the submit order button, the javascript code must validate everything entered by the user. Please help finnish this...
1
by: runway27 | last post by:
i have 3 pages page1.php page2.php and page3.php in page1.php user fills a registration form in page2.php the user can review what they have entered and make any changes and page3.php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.