473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

REQ: New Question Form ( new JavaScript Student )

Sue
In this code why is it that when I press the SUBMIT button the focus
only goes back to the Numeric field. What do I need to do to correct
this problem?

Sue


<html>

<head>
<title>JavaScri pt Project</title>
<SCRIPT LANGUAGE="JAVAS CRIPT">
<!--Hide from old browsers
function Validate() {
var themessage = "You are required to complete the following fields:
";

// validate the Firstname
if (document.Regis ter.FirstName.v alue=="") {
alert("Please enter your Firstname");
document.Regist er.FirstName.va lue="";
document.Regist er.FirstName.fo cus();
}

else {
// validate the Lastname
if (document.Regis ter.LastName.va lue=="") {
alert("Please enter your Lastname");
document.Regist er.LastName.val ue="";
document.Regist er.LastName.foc us();
}
else {

// validate Age as be numeric
var YearsOld=docume nt.Register.Age .value;
var YearsOld=parseI nt(YearsOld,10) ;
if (isNaN(YearsOld )) {
alert("Age is not numeric");
document.Regist er.Age.value="" ;
document.Regist er.Age.focus();
}
else {

// validate the @ sign and the period as being the fourth
from the last character in an e-mail address
var RegeMail=docume nt.Register.ema il.value;
var atSign = RegeMail.indexO f("@");

var Period=document .Register.email .value;
var PPeriod = Period.indexOf( '.');
var LPeriod = Period.length - 4;

if (RegeMail == "" || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address");
document.Regist er.email.value = "";
document.Regist er.email.focus( );
}
else{

// validate the Gender in a drop down menu
var sex=document.fo rms[0].Gender.selecte dIndex;
if (sex==0) {
alert("You must select your GENDER from the drop-down
Menu.");
document.forms[0].Gender.focus() ;
}
else
{

//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields:
") {
document.Regist er.submit();
}
else {
alert(themessag e);
return false;
}
}
}
}
}
}
}

//-->
</script>
</head>

<body>

<center>

<form name=Register method="post" action="">
<table border="0" width="90%">

<!-- Begining of the first line of the form for entering data. -->
<tr>
<td>Enter Your Firstname :</td><td align="center">
<Input Type="text" Name="FirstName " value=" "></td>
<td>&nbspEnte r Your Age :</td> <td align="center">
<Input Type="numeric" Name="Age" value=" "></td>
<td align="center"> Select your : <SELECT NAME="Gender" SIZE=1 >
<OPTION SELECTED VALUE=""> --- Select Gender ---
<OPTION VALUE="Male">Ma le
<OPTION VALUE="Female"> Female
</SELECT>
</td>
</tr>
<!-- ending of the first line of the form for entering data. -->
<tr>
<td></td>
</tr>

<!-- Begining of the second line of the form for entering data. -->
<tr>
<td align="center"> Enter Your Lastname :</td><td align="center">
<Input Type="text" Name="LastName" value=" "></td>
<td align="center"> Enter Your Email Address :</td> <td
align="center">
<Input Type="text" Name="email" value=" "></td>
<td align="right">< input type=button value="Submit Request"
onclick="Valida te();">
<Input Type="Reset"></td>
</tr>
<!-- ending of the second line of the form for entering data. -->

</body>
</html>
Jul 20 '05 #1
8 1722
Lee
Sue said:

In this code why is it that when I press the SUBMIT button the focus
only goes back to the Numeric field. What do I need to do to correct
this problem?
In the HTML, you set the initial value of the fields to a single space
character:
<Input Type="text" Name="LastName" value=" "></td>


Your Validate() code checks to see if the fields are empty.
A single space character is not the same as an empty field.
Change your HTML value attributes to "".

Jul 20 '05 #2
Sue
On 10 Dec 2003 08:16:18 -0800, Lee <RE************ **@cox.net> wrote:
Sue said:

In this code why is it that when I press the SUBMIT button the focus
only goes back to the Numeric field. What do I need to do to correct
this problem?


In the HTML, you set the initial value of the fields to a single space
character:
<Input Type="text" Name="LastName" value=" "></td>


Your Validate() code checks to see if the fields are empty.
A single space character is not the same as an empty field.
Change your HTML value attributes to "".


Hey Lee,

Thanks for explaining that to me. Now one last question before I have
to turn this project in on Friday.

This is not necessary for my project but I have created a function to
do more extensive checking for numbers in the Age field. However, I am
unable to insert this function into the code without it causing
problems. Is there an easy fool proof way to call this function
without a lot of difficulty. Thanks

Sue
function CheckAge(Age) {
var valid = "0123456789 "
var Good = "yes";
var Check;
for (var i=0; i<Age.value.len gth; i++) {
Check = "" + Age.value.subst ring(i, i+1);
if (valid.indexOf( Check) == "-1") Good = "no";
}
if (Good == "no") {
alert("An INVALID Character has been entered for your age !");
document.Regist er.Age.value="" ;
document.Regist er.Age.focus();
}
}
Jul 20 '05 #3
Sue wrote:
This is not necessary for my project but I have created a function to
do more extensive checking for numbers in the Age field. However, I am
unable to insert this function into the code without it causing
problems. Is there an easy fool proof way to call this function
without a lot of difficulty. Thanks
[...]
function CheckAge(Age) {
function checkAge(age)
{

(Note that the case changed for good style.)
var valid = "0123456789 "
var Good = "yes";
Do not use strings for booleans, use booleans.

var good = true;

But you will not need that initialization and the `valid'
variable if you use Regular Expressions and write
var Check;
var check;
for (var i=0; i<Age.value.len gth; i++) {
Check = "" + Age.value.subst ring(i, i+1);
check = age.charAt(i);

is, although not required for the RegExp solution, far more simple.
Even with String.substrin g(...) the concatenation with "" is not
required since String.substrin g(...) already returns a `string' value.
if (valid.indexOf( Check) == "-1") Good = "no";
}
var good = /^\d+$/.test(age); // Only one or more decimal digit?
if (Good == "no") {
if (! good)
{
alert("An INVALID Character has been entered for your age !");
document.Regist er.Age.value="" ;
document.Regist er.Age.focus();
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}
}

HTH

PointedEars
Jul 20 '05 #4
Sue
On Thu, 11 Dec 2003 22:30:03 +0100, Thomas 'PointedEars' Lahn
<Po*********@we b.de> wrote:
Sue wrote:
This is not necessary for my project but I have created a function to
do more extensive checking for numbers in the Age field. However, I am
unable to insert this function into the code without it causing
problems. Is there an easy fool proof way to call this function
without a lot of difficulty. Thanks
[...]
function CheckAge(Age) {

<snip>

Thomas,

I tried to make the changes you suggested but some of this is over my
head. The charAt(i) has not been in any of the chapters that I have
had to do and I have done every chapter in the book this quarter. For
self gratification I would like to be able to plug this code into my
project but I have been unable to plug it in with out it causing
problems and my time is up tomorrow (Friday).

function checkAge(age) {
var valid = "0123456789 "
var good = true;
var check;
for (var i=0; i<Age.value.len gth; i++) {
check = age.charAt(i);
var good = /^\d+$/.test(age); // Only one or more decimal digit?
}
if (! good) {
alert("An INVALID Character has been entered for your age !");
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}
Jul 20 '05 #5
[posted & mailed (the latter accidentally, though)]

Sue wrote:
I tried to make the changes you suggested but some of this is over my
head. The charAt(i) has not been in any of the chapters that I have
had to do and I have done every chapter in the book this quarter.
It looks like you have one of the plenty of badly written JavaScript
books out there (see also the FAQ, there is only *one* which can be
recommended): charAt(...) is a *basic* method of the String prototype,
returning the character of the respective String (object) at the index
`i' (where the index is zero-based). Throw the book away and read the
specs instead (as you see, you can download the entire Guide/Reference
to your local hard disk; I have reconfigured my local Web server for
quick access which I can recommend you, too):

http://devedge.netscape.com/library/manuals/
For self gratification I would like to be able to plug this code into
my project but I have been unable to plug it in with out it causing
problems and my time is up tomorrow (Friday).


Search the index of the JavaScript 1.x Core Guide/Reference, it is
the fastest way to find what you are looking for. Here's the working
function (only snipped the parts no longer necessary):

function checkAge(age) {
var good = /^\d+$/.test(age); // Only one or more decimal digit?
if (! good)
{
alert("An INVALID Character has been entered for your age!");
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}

A little bit more optimization is possible, removing another unnecessary
variable:

function checkAge(age) {
// Does it contain a character other than a decimal digit?
if (! /^\d+$/.test(age))
{
alert("An INVALID Character has been entered for your age!");
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}

I guess you need that for form validation, so you would instead write:

<script type="text/javascript">
<!--
function checkAge(oAge) { // pass a reference to the obj. 2b checked
// Does it contain a character other than a decimal digit?
if (! /^\d+$/.test(oAge.valu e))
{
alert("An INVALID Character has been entered for your age!");
oAge.value = "";

if (typeof oAge.focus == "function"
|| typeof oAge.focus == "object")
{
oAge.focus();
}

return false;
}

return true;
}
//-->
</script>

<form action="..." onsubmit="retur n checkAge(this.e lements['Age']);">
...
<input name="Age">
...
</form>

You should always pass references to a form-processing function instead
of primitive values as they are not that error-catching and you can use
them also within the function, without retrieving them (via collections)
again.
HTH

PointedEars
--
Dr. Weaver: What's with the cat?
Cop in Morgue: Well, there's a problem with the cat. Sign here.
Dr. Weaver: [signing] What's the problem with the cat?
Cop in Morgue: It's your problem.
Jul 20 '05 #6
Sue
On Fri, 12 Dec 2003 05:35:49 +0100, Thomas 'PointedEars' Lahn
<Po*********@we b.de> wrote:
[posted & mailed (the latter accidentally, though)]

Sue wrote:
I tried to make the changes you suggested but some of this is over my
head. The charAt(i) has not been in any of the chapters that I have
had to do and I have done every chapter in the book this quarter.


It looks like you have one of the plenty of badly written JavaScript
books out there (see also the FAQ, there is only *one* which can be
recommended) : charAt(...) is a *basic* method of the String prototype,
returning the character of the respective String (object) at the index
`i' (where the index is zero-based). Throw the book away and read the
specs instead (as you see, you can download the entire Guide/Reference
to your local hard disk; I have reconfigured my local Web server for
quick access which I can recommend you, too):

http://devedge.netscape.com/library/manuals/
For self gratification I would like to be able to plug this code into
my project but I have been unable to plug it in with out it causing
problems and my time is up tomorrow (Friday).


Search the index of the JavaScript 1.x Core Guide/Reference, it is
the fastest way to find what you are looking for. Here's the working
function (only snipped the parts no longer necessary):

function checkAge(age) {
var good = /^\d+$/.test(age); // Only one or more decimal digit?
if (! good)
{
alert("An INVALID Character has been entered for your age!");
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}

A little bit more optimization is possible, removing another unnecessary
variable:

function checkAge(age) {
// Does it contain a character other than a decimal digit?
if (! /^\d+$/.test(age))
{
alert("An INVALID Character has been entered for your age!");
var oAge = document.forms["Register"].elements["Age"];
oAge.value = "";
oAge.focus();
}
}

I guess you need that for form validation, so you would instead write:

<script type="text/javascript">
<!--
function checkAge(oAge) { // pass a reference to the obj. 2b checked
// Does it contain a character other than a decimal digit?
if (! /^\d+$/.test(oAge.valu e))
{
alert("An INVALID Character has been entered for your age!");
oAge.value = "";

if (typeof oAge.focus == "function"
|| typeof oAge.focus == "object")
{
oAge.focus();
}

return false;
}

return true;
}
//-->
</script>

<form action="..." onsubmit="retur n checkAge(this.e lements['Age']);">
...
<input name="Age">
...
</form>

You should always pass references to a form-processing function instead
of primitive values as they are not that error-catching and you can use
them also within the function, without retrieving them (via collections)
again.
HTH

PointedEars

Thanks Thomas,

I check out your url and recommend to the instructor that he list is
as an aid in future class.

Sue
Jul 20 '05 #7
Sue <Su***@comcast. net > writes:
Thanks Thomas,
Please reduce your quotes. There is no need to include 96 lines of quotes
to add four lines of answer.
I check out your url and recommend to the instructor that he list is
as an aid in future class.


One or two lines of quotes would be sufficient to give the context needed
to understand your answer.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
Sue wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
[...]
Sue wrote:
I tried to make the changes you suggested but some of this is over my
head. The charAt(i) has not been in any of the chapters that I have
had to do and I have done every chapter in the book this quarter.
It looks like you have one of the plenty of badly written JavaScript
books out there (see also the FAQ, there is only *one* which can be
recommended): charAt(...) is a *basic* method of the String prototype,
returning the character of the respective String (object) at the index
`i' (where the index is zero-based). Throw the book away and read the
specs instead (as you see, you can download the entire Guide/Reference
to your local hard disk; I have reconfigured my local Web server for
quick access which I can recommend you, too):

http://devedge.netscape.com/library/manuals/
[...]


Thanks Thomas,


You are welcome, but please trim your quotes to the part(s) you are
actually referring to the next time. Time, bandwidth and disk space
are precious resources.
I check out your url and recommend to the instructor that he list is
as an aid in future class.


Good idea.
PointedEars
Jul 20 '05 #9

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

Similar topics

8
1974
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am not expected to check for everything. At any rate, the problem I am having with the following code is that it does not clear the fields once I press the SEND button. So can anyone here enlighten me as to what is causing the problem.
5
2692
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
10
3587
by: Steve Benson | last post by:
Our regular programmer moved on. I'm almost clueless in Javascript/ASP and got the job of adapting existing code. In the page below, everything works until I added the function checkIt() to validate which radio button was clicked and what was in a textfield. The form is an attendance checking page for a cyber charter school. What I'm trying to accomplish is that if a parent marks the student present, there should not be anything in the...
8
1987
by: shandain | last post by:
Ok, I have done this a million times, and I can't get this to work... I hope I am just being an idiot and missing something... but I can't see it... so please call me an idiot and tell me what it is... <code> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <title>Online Orientation Quiz</title> <meta http-equiv="Content-Type" content="text/html;
4
2146
by: Terry | last post by:
I need some help refining an MS 2000 relational databse. I have created a simple relational database using two tables, 'Student Details', 'Exam Details' and two forms, 'Input/Edit Exam Details', 'Input/Edit Student Details'. 'Student Details' has a field called 'Log Book No' (no duplicates allowed) and this is the Primary Key. 'Exam Details' also has a field called 'Log Book No' (duplicates allowed) and has no Primary Key, (as each...
15
2303
by: Paul T. Rong | last post by:
Hi everybody, This time a very very difficult case: I have a table "tblStudent", there are 50 students, I also made a form "frmStudent" based on "tblStudent", now if I don't want to show all the students in a classical (vertically, from top to bottom) way, but make them "sitting" in 50 text boxes, 5 rows 10 columns, how to do it? Thanks in advance.
5
2469
by: Phil Powell | last post by:
Requirement is to refresh a page in the form of a continual form submittal (for server-side validation and action) Here is the Javascript I came up with that I thought would do that: <script type="text/javascript"> function generateForm() { document.forms.elements.name = 'username'; document.forms.elements.type = 'hidden'; document.forms.elements.value = 'ppowell'
5
1820
toxicpaint
by: toxicpaint | last post by:
Hi there. I can't seem to work out what I'm doing wrong here and I was wondering if you could help. I have a form to submit a question, but as the questions vary in nature, I want them to go to 3 or more e-mail addresses. I'm using a drop down menu to select the type of question the user wants to ask then using this to determine what e-mail address to pick. The cgi on the server is configured to only send an e-mail if the "mailto" field...
0
2179
by: jianxin9 | last post by:
Hi everyone, I don't have a lot of experience with ASP and I was hoping someone could help me. I want to use our ASP form along with some javascript code to create a form where our patrons can select which department they will send the form to (we are trying to consolidate forms). This is what I have so far, but when I test the form, I keep getting this error message: Mailing Failed... Error is: FromAddress Property cannot be blank. You...
0
8828
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
9537
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
9367
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
9319
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
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
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...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.