473,394 Members | 1,715 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,394 software developers and data experts.

Validating a drop down (select) type, form element

Hi everybody:
I am trying to write a generic function, isValidMonth, that can used on 2
form elements, smonth and emonth. I would like the function to test whether
the user has selected a month or not.
If anyone can help me I would really appreciate it. What I have done
follows.

My form has:

<SELECT name=smonth>
<OPTION value=0>-Month-
<OPTION value=1>Jan
<OPTION value=2>Feb
<OPTION value=3>Mar
...
<OPTION value=12>Dec
</SELECT>

<SELECT name=emonth>
<OPTION value=0>-Month-
<OPTION value=1>Jan
<OPTION value=2>Feb
<OPTION value=3>Mar
...
<OPTION value=12>Dec
</SELECT>
..
My JavaScript contains:

function isValidMonth(month) {
if (month[month.selectedIndex].value == 0
return false
return true
}
function submitIt(form) {
if (!isValidmonth(smonth))
alert("You have not selected a start month")
return true
}

Thank you

Terry
Jul 20 '05 #1
3 1569
In article <Q6*******************@news04.bloor.is.net.cable.r ogers.com>,
tg******@rogers.com enlightened us with...

<SELECT name=smonth>
quote attributes.
sname="month"
<OPTION value=0>-Month- value="0"
.
My JavaScript contains:

function isValidMonth(month) {
if (month[month.selectedIndex].value == 0
return false
return true
}

Better programming practice (only one exit) and a correction

function isValidMonth(month) {
b = true;
if (month.options[month.selectedIndex].value == 0
b = false
return b
}

function submitIt(form) {
if (!isValidmonth(smonth))
alert("You have not selected a start month")
return true
}


return value needed; keyword changed; correction to call

function submitIt(frm) {
b = true;
if (isValidmonth(frm.smonth))
{
alert("You have not selected a start month");
b = false;
}
return b
}

handler called in onSubmit of form, not onClick of submit

<form name="f1" action="whatever" method="post" onSubmit="return
submitIt(this)">

--
--
~kaeli~
A little rudeness and disrespect can elevate a meaningless
interaction to a battle of wills and add drama to an
otherwise dull day.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2
kaeli <ti******@NOSPAM.comcast.net> writes:
Better programming practice (only one exit) and a correction
I'm not sure I agree with that practice in all cases.
function isValidMonth(month) {
b = true;
if (month.options[month.selectedIndex].value == 0
Missing ")". I recommend { ... } around all if branches, even if they
are only a single statement. It makes it easier to extend them later.

From the original poster's description, I would just test:
month.selectedIndex == 0
since his invalid option was the first one.
b = false
return b
}


.... but when it comes to returning booleans with if-statements,
I always want to do it shorter:

function isValidMonth(month) {
return month.selectedIndex != 0;
}

Otherwise, I agree.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Thanks Kaeli. I think a correction is needed for the code that you provided
me with.
if (!isValidmonth(frm.smonth)) should replace if
(isValidmonth(frm.smonth)) inside of function submitIt(frm).

Terry

"kaeli" <ti******@NOSPAM.comcast.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <Q6*******************@news04.bloor.is.net.cable.r ogers.com>,
tg******@rogers.com enlightened us with...

<SELECT name=smonth>


quote attributes.
sname="month"
<OPTION value=0>-Month-

value="0"
.
My JavaScript contains:

function isValidMonth(month) {
if (month[month.selectedIndex].value == 0
return false
return true
}


Better programming practice (only one exit) and a correction

function isValidMonth(month) {
b = true;
if (month.options[month.selectedIndex].value == 0
b = false
return b
}

function submitIt(form) {
if (!isValidmonth(smonth))
alert("You have not selected a start month")
return true
}


return value needed; keyword changed; correction to call

function submitIt(frm) {
b = true;
if (isValidmonth(frm.smonth))
{
alert("You have not selected a start month");
b = false;
}
return b
}

handler called in onSubmit of form, not onClick of submit

<form name="f1" action="whatever" method="post" onSubmit="return
submitIt(this)">

--
--
~kaeli~
A little rudeness and disrespect can elevate a meaningless
interaction to a battle of wills and add drama to an
otherwise dull day.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Samir | last post by:
Say I have 4 forms, all four have different numbers of text boxes. is there a script that I can use to check to make sure everything on the form is not blank?
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
5
by: mcraven.2 | last post by:
I know this works on all objects except drop down boxes. doc = document.all for (i=0;i<doc.length;i++) { doc(i).style.cursor = 'wait'; } Is it possible to make a cursor into an hourglass...
4
by: teknoshock | last post by:
I have created a page with multiple drop down boxes, all populated with the same options. My problem is, for 12 dropdown boxes and 40 choices per box, I end up with a massive file. Also, if I...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
2
by: phpnewbie26 | last post by:
I currently have two drop down menus where the second one is populated from the first one. My second drop down menu should be able to do multiple selection. I have searched online and found out how...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.