473,788 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form validation script problem - your help needed

Ray
Hi all,

I'm new to JavaScript and am trying to create a client side JavaScript form
validation script. I'm doing ok with validating text input boxes but have
a problem that I have not been able to solve and one I hope someone will
help me resolve.

I want to check the contents of a drop down box and if a certain item is
selected, check the contents of a text input box to see if a any text was
entered. If no text is entered, I want to put up a notice so the user
won't forget.

Here is a code segment that does work:

if (feedbackform.s ervices.value == 'none selected') {
alert("Please provide us which service option you are interested
in.");
feedbackform.se rvices.focus();
return false;
}
And here is the segment of code that to me looks ok, but it will not work
as expected.

if (feedbackform.s ervices.value == 'Existing website redesign' &&
feedbackform.cu rrenturl.value. length == 0)
{
alert("Please provide us with a URL");
feedbackform.cu rrenturl.focus( );
return false;
}
Any help appreciated.

Ray

Jul 23 '05 #1
7 1414
Ra*@goodnewsweb design.com wrote:
Hi all,

I'm new to JavaScript and am trying to create a client side JavaScript form
validation script. I'm doing ok with validating text input boxes but have
a problem that I have not been able to solve and one I hope someone will
help me resolve.

I want to check the contents of a drop down box and if a certain item is
selected, check the contents of a text input box to see if a any text was
entered. If no text is entered, I want to put up a notice so the user
won't forget.

Here is a code segment that does work:

if (feedbackform.s ervices.value == 'none selected') {
alert("Please provide us which service option you are interested
in.");
feedbackform.se rvices.focus();
return false;
}
And here is the segment of code that to me looks ok, but it will not work
as expected.

if (feedbackform.s ervices.value == 'Existing website redesign' &&
feedbackform.cu rrenturl.value. length == 0)
{
alert("Please provide us with a URL");
feedbackform.cu rrenturl.focus( );
return false;
}
Any help appreciated.

Ray


Given that your logic depends on what is coded in the HTML, we need
to see the HTML related to your script. You have also left us guess
as to what 'will not work as expected' is, nor which browser you are
developing & testing in (sometimes it matters, sometimes not).

I'll guess that the alert does not show up when you don't put any
text into 'feedbackform.c urrenturl'. The script in that regard looks
fine.

The other part: feedbackform.se rvices.value - infers that you are
trying to get the value of a select element. In that case, you may
be depending on the value of the select being the text of the
selected option because you haven't given it a value attribute -
that is what the HTML spec says and is how things work in some
browsers (e.g. Firefox) but not others (e.g. IE).

But you have set 'Existing website redesign' as the value, or it's
a text input and the value gets there some other way, ...

Maybe you've mis-typed a form element name, or ...

--
Rob
Jul 23 '05 #2
Ra*@goodnewsweb design.com wrote:
And here is the segment of code that to me looks ok, but it will not
work as expected.

if (feedbackform.s ervices.value == 'Existing website redesign' &&
feedbackform.cu rrenturl.value. length == 0)
{
alert("Please provide us with a URL");
feedbackform.cu rrenturl.focus( );
return false;
}


Assumed that "feedbackfo rm" is a valid reference to a form, try:

var mySelect, myTextInput;
if ((mySelect = feedbackform.el ements['services'])
&& mySelect.option s[mySelect.select edIndex].value
== 'Existing website redesign'
&& (myTextInput = feedbackform.el ements['currenturl'])
&& !myTextInput.va lue
) {
window.alert("P lease provide us with a URL");
if (myTextInput.fo cus) {
myTextInput.foc us();
}
return false;
}

ciao, dhgm
Jul 23 '05 #3
R...@goodnewswe bdesign.com wrote:
Hi all,

I'm new to JavaScript and am trying to create a client side JavaScript form validation script. I'm doing ok with validating text input boxes but have a problem that I have not been able to solve and one I hope someone will help me resolve.

I want to check the contents of a drop down box and if a certain item is selected, check the contents of a text input box to see if a any text was entered. If no text is entered, I want to put up a notice so the user won't forget.

Here is a code segment that does work:

if (feedbackform.s ervices.value == 'none selected') {
alert("Please provide us which service option you are interested in.");
feedbackform.se rvices.focus();
return false;
}
And here is the segment of code that to me looks ok, but it will not work as expected.

if (feedbackform.s ervices.value == 'Existing website redesign' &&
feedbackform.cu rrenturl.value. length == 0)
{
alert("Please provide us with a URL");
feedbackform.cu rrenturl.focus( );
return false;
}
Any help appreciated.

Ray


Just pilin' on here...

function checkform(els)
{
if (els.services.s electedIndex == 3
&& /^\s*$/.test(els.curre nturl.value))
{
alert("Please provide us with a URL");
if (els.currenturl .focus)
els.currenturl. focus(*);
return false;
}
return true;
}

<form name="feedbackf orm"...onsubmit ="return checkform(this. elements)">

The selectedIndex of '3' refers to the fourth option (OK, you knew
that). The argument against using it instead of the option value is:
additions to the form may render it incorrect. For a simple application
like this, however, the simplicity is appealing.

Jul 23 '05 #4
RobB wrote:
[...]
Just pilin' on here...

function checkform(els)
{
if (els.services.s electedIndex == 3
&& /^\s*$/.test(els.curre nturl.value))
{
alert("Please provide us with a URL");
if (els.currenturl .focus)
els.currenturl. focus(*);
return false;
}
return true;


'Stacks on the mill' I believe it's called...

Maybe we can crystal-ball one step further and guess that the OP also
requires a URL validation test - simply testing for something or
nothing isn't exactly rigorous... :-x

I'll kick it off... (I've kept the invented '3')

if (els.services.s electedIndex == 3
&& /^http[s]?:\/\/\w+/.test(els.curre nturl.value))
{

Is likely as tough as it should get. Test any tighter and things
like anchors or query strings get rejected (or maybe they should?).
[...]
--
Rob
Jul 23 '05 #5
ray
On Fri, 15 Apr 2005 01:28:26 +0200, "Dietmar Meier"
<us************ ***@innoline-systemtechnik.d e> wrote:
Ra*@goodnewswe bdesign.com wrote:
And here is the segment of code that to me looks ok, but it will not
work as expected.

if (feedbackform.s ervices.value == 'Existing website redesign' &&
feedbackform.cu rrenturl.value. length == 0)
{
alert("Please provide us with a URL");
feedbackform.cu rrenturl.focus( );
return false;
}


Assumed that "feedbackfo rm" is a valid reference to a form, try:

var mySelect, myTextInput;
if ((mySelect = feedbackform.el ements['services'])
&& mySelect.option s[mySelect.select edIndex].value
== 'Existing website redesign'
&& (myTextInput = feedbackform.el ements['currenturl'])
&& !myTextInput.va lue
) {
window.alert("P lease provide us with a URL");
if (myTextInput.fo cus) {
myTextInput.foc us();
}
return false;
}

ciao, dhgm


Hi Dietmar and RobG and RobB,

Thanks for replying!

Dietmar, I tried inserting your code, but it did not work. The form itself
works and I do receive feedback through it. However, I decided to add a
little bit of client side checking because of the number of forms I get
with one field or another left blank accidently. I am testing the
scripts on IE 5.5 and on Firefox 1.0.2 and the page behaves the same on
both.

If I didnt' make myself clear before, what I'm trying to achieve is that if
the user selects website redesign from a drop down menu selector, I want to
check (at submit time) whether he has entered a URL of his current site.

The page I'm working on can be found here:

http://goodnewswebdesign.com/testing/contact.html
Raymond Cantillon
(Owner - Good News Web Design)
--
------------------------------------------------------------------------
Put your church or other christian based organization
on the internet today! Visit our website:
http://www.goodnewswebdesign.com
A Christian Web Design Company
------------------------------------------------------------------------
Jul 23 '05 #6
ra*@goodnewsweb design.com wrote:
&& mySelect.option s[mySelect.select edIndex].value
== 'Existing website redesign'
Dietmar, I tried inserting your code, but it did not work.


Maybe that's because you simply copied & pasted it?

"Existing website redesign" is a value of the text property, not
the value property, of one of your select's options. So either
replace ".value" with ".text", or replace "== 'Existing website
redesign'" with "== '4'".

ciao, dhgm
Jul 23 '05 #7
Ray
On Mon, 18 Apr 2005 15:57:22 +0200, "Dietmar Meier"
<us************ ***@innoline-systemtechnik.d e> wrote:
ra*@goodnewswe bdesign.com wrote:
&& mySelect.option s[mySelect.select edIndex].value
== 'Existing website redesign'
Dietmar, I tried inserting your code, but it did not work.


Maybe that's because you simply copied & pasted it?


This is true. I changed it as you suggested below and it now works
beautifully! Thank you for your kind assistance!

Ray
"Existing website redesign" is a value of the text property, not
the value property, of one of your select's options. So either
replace ".value" with ".text", or replace "== 'Existing website
redesign'" with "== '4'".

ciao, dhgm


--
------------------------------------------------------------------------
Put your church or other christian based organization
on the internet today! Visit our website:
http://www.goodnewswebdesign.com
A Christian Web Design Company
King James Bible - kingjamesbible. goodnewswebdesi gn.com
Catholic Catechism - catholiccatechi sm.goodnewswebd esign.com
------------------------------------------------------------------------
Jul 23 '05 #8

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

Similar topics

5
4261
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the second form when it is called. Both forms are .htm files that call themselves when the submit button is press via the following command in each form: <form method="post" action="<?php $server?>">
3
1823
by: Amir | last post by:
I'm looking for an example of form validation by JavaScript before it's processed by a PHP script. Any help will be appreciated.
11
8757
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
16
2252
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
7
6999
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file. When the form was correct, I tried loading the next page by using <META http-equiv refresh>. But...
11
3002
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
3
3300
uranuskid
by: uranuskid | last post by:
Hey folks, I was going to include a contact form on my website. Well, in the first place that seemed an easy thing to do with a form that prompts a PHP file validating the input vaiables and using it's mail () function to send the mail. However, as I got more into that topic I realized that one should be really concerned about the validation part to prevent spam abuse. There are shiploads of 'mail scripts' available with each of them has...
13
3612
by: Andrew Falanga | last post by:
HI, Just a warning, I'm a javascript neophyte. I'm writing a function to validate the contents of a form on a web page I'm developing. Since I'm a neophyte, this function is quite simple at this time (in fact, I don't even know if it totally works, I'm still debugging). However, the first problem is that when an error is encountered, I get my alert box, I press ok and then the form is submitted and the new data is entered into the...
3
1842
by: arty | last post by:
So basically i have a form with a action attribute set to another form, it is a survey spread among several pages and using php sessions, now im trying to validate the form with Javascript but it looks imposible because the check boxes all have the same name Q1(and the array symbol is conflicting with js), the same name is needed for processing the answers with php in the database, i am able to make a form validation of the form below using...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.