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

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>JavaScript Project</title>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
function Validate() {
var themessage = "You are required to complete the following fields:
";

// validate the Firstname
if (document.Register.FirstName.value=="") {
alert("Please enter your Firstname");
document.Register.FirstName.value="";
document.Register.FirstName.focus();
}

else {
// validate the Lastname
if (document.Register.LastName.value=="") {
alert("Please enter your Lastname");
document.Register.LastName.value="";
document.Register.LastName.focus();
}
else {

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

// validate the @ sign and the period as being the fourth
from the last character in an e-mail address
var RegeMail=document.Register.email.value;
var atSign = RegeMail.indexOf("@");

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.Register.email.value = "";
document.Register.email.focus();
}
else{

// validate the Gender in a drop down menu
var sex=document.forms[0].Gender.selectedIndex;
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.Register.submit();
}
else {
alert(themessage);
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>&nbspEnter 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">Male
<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="Validate();">
<Input Type="Reset"></td>
</tr>
<!-- ending of the second line of the form for entering data. -->

</body>
</html>
Jul 20 '05 #1
8 1676
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.length; i++) {
Check = "" + Age.value.substring(i, i+1);
if (valid.indexOf(Check) == "-1") Good = "no";
}
if (Good == "no") {
alert("An INVALID Character has been entered for your age !");
document.Register.Age.value="";
document.Register.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.length; i++) {
Check = "" + Age.value.substring(i, i+1);
check = age.charAt(i);

is, although not required for the RegExp solution, far more simple.
Even with String.substring(...) the concatenation with "" is not
required since String.substring(...) 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.Register.Age.value="";
document.Register.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*********@web.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.length; 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.value))
{
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="return checkAge(this.elements['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*********@web.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.value))
{
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="return checkAge(this.elements['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/rasterTriangleDOM.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
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...
5
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...
10
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...
8
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...
4
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',...
15
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...
5
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...
5
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...
0
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...

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.