473,394 Members | 2,048 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.

Form validation and combo boxes

I have a form with 3 text fields (one of which is a zip code) and 5
combo boxes. The combo boxes are all set with the first value as
'selected' when the page is first displayed. The 3 text fields are
required and are by default empty. I need to validate that the text
fields have an entry and that the zip code is numeric and the correct
length. If the form fails validation - one of the text fields is empty,
for example - I need to alert the user and leave the combo boxes as
they were when the submission occurred. In other words, I don't want
the user to have to reselect the combo box values after validation
fails. I'm new at this - usually do it server-side, but the boss wants
to use JavaScript. Can someone please point me to an example. I've
Googled to no avail.

Thanks!

Nov 22 '06 #1
2 4247
ddog wrote:
I have a form with 3 text fields (one of which is a zip code) and 5
combo boxes. The combo boxes are all set with the first value as
'selected' when the page is first displayed. The 3 text fields are
required and are by default empty. I need to validate that the text
fields have an entry and that the zip code is numeric and the correct
length. If the form fails validation - one of the text fields is empty,
for example - I need to alert the user and leave the combo boxes as
they were when the submission occurred. In other words, I don't want
the user to have to reselect the combo box values after validation
fails. I'm new at this - usually do it server-side, but the boss wants
to use JavaScript.
<script type="text/javascript">
function fch() {
if ( document.forms[0].c4.value == '' ||
document.forms[0].c5.value == '' ||
!/^\d{5}$/.test(document.forms[0].c3.value)
)
{
alert('Form is not complete, please try again.');
return false;
}
}
</script>

<form method="post" action="script.asp" onSubmit="return fch()">
<select size="1" name="c1">
<option value="one" selected>one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
<hr>
<select size="1" name="c2">
<option value="A" selected>A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<hr>
<input type="text" name="c3" value="" maxlength="5"(ZIP)
<hr>
<input type="text" name="c4" value="">
<hr>
<input type="text" name="c5" value="">
<hr>
<input type="submit">

Hope this helps,

--
Bart

Nov 22 '06 #2
VK

ddog wrote:
I don't want
the user to have to reselect the combo box values after validation
fails. I'm new at this - usually do it server-side <snip>
Client-side it is called "if validation fails, I don't want to submit
the form" :-) And if submission is cancelled, then all form elements
have the states they have.

To cancel form submission, form onsubmit intinsic event handler has to
return false. This way:

<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function validate(frm) {
var ret = true;
if (frm.data.value == '') {
ret = false;
}
// other checks
// more checks
return ret;
}
</script>
</head>
<body>
<form name="MyForm"
method="POST" action="someURL"
onsubmit="return validate(this)">
<input type="text" name="data">
<input type="submit">
</body>
</html>

For a particular type of validation there is a great number of modules
written. Google for "javascript form validation" or look for a sample
at:
<http://www.mattkruse.com/javascript/validations/>

P.S. There is a silly fashion recently (brought by ASP coders AFAICT)
to serve "minimum code" instead of normal HTML pages, like say
<form>...</formblocks only without html, head and body. While browser
error correction mechanics makes such "pages" still usable, client-side
DOM scripting can get nucked or damaged, in the particular with form
handling. I'm *not* saying at all that you are doing something like
that, just an abstract mention.

Nov 22 '06 #3

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

Similar topics

8
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time...
1
by: FZ | last post by:
Hi Gang, I was wondering if a generous person might be able to walk me through what I believe is a pretty simple task. I actually have significant Access experience, but I haven't done it in...
2
by: misschristalee | last post by:
I'm having a brain blockage day.... Scenario: Search Form with 6 text boxes Query has same six fields Each has this IIF: IIf(IsNull(!!),"",!!) with each dictating the correct text box of...
2
by: Sean | last post by:
Greetings all, I am attempting to make a form that will filter through several tables that (I believe) have refretial integrity. I am pulling data from several tables into the form and i would...
2
by: Mr Man | last post by:
Is it possible to set the combo-box drop down list default to zero or blank. Sometimes when you first view it, or not selected any data the box has a selection in it, so you're not sure whether...
1
by: Dave | last post by:
Hello all, First I'd like to apologize...This post was meant to be put in my previous post, but I tried many times without success to reply within my previous post. Now here goes... I have a...
6
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate...
2
by: Margie | last post by:
Hello again After getting great help perfecting my little Access 2007 movies database, I've stumbled upon an inconvenience. In my opinion it seems basic, but still I'm not able to solve it. As an...
4
kcdoell
by: kcdoell | last post by:
I have a form (Form1) that displays records based on 3 unbound combo boxes that are also on my form. My record source for this form is a query “ReQryForecast” (which looks at those combo boxes via...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.