...

View Full Version : JavaScript only works once



Beverly
07-11-2011, 02:00 PM
Hi I am new to programming have tried to write a simple HTML page and servlet. Using tomcat.

Added a javascript validation function.

However when I type wrong it works the first time, displays alert and returns to entry form.

on second attempt type in wrong again, expected to see alert and return to entry form but it submits.

I am new to programmng and could do with some help can not find any clue, answer to this problem. Have checked javascript is ticked in Firefox 5.0.

Set up is I have a HTML page with one javascript check function at present for test development. It is linked to a servlet in tomcat.

Problem is when I enter wrong the first time, get alert message and it returns to the HTML orginal page.
However if I click on entry field and enter wrong again. It submits the form, does not give an alert or return to the orginal form?

Has any one got a solution or is this how validation should work.

Code is:


<HTML><head><title>Creation of POs</title>
<H1>Purchase Order</h1>
<script type="text/javaScript">
function check() {
check = document.forms[0].SLine1.value;
if(check=="wrong"){
alert("enter correct info");
return false;
} else {
return true; }
}
</script></head><body bgcolour="yellow">
<form method="GET" action="Distest_session30a_vs1" onSubmit="return
check()">
various inputboxes and messages
<table>
<tr><td><input type="text" name="SLine1"></td></tr>
various other lines of inputs boxes
</table>
<Input type="submit" value="Send now">
</form></body></html>

Thanks in advance for any help people can give spent a lot of time on this already and can think of nothing else to try to resolve.

Using firefox 5.0, check and javascript is enabled?

DaveyErwin
07-11-2011, 02:34 PM
function check() {
check = document.forms[0].SLine1.value;

You are chaning the value of check to
a form elemennt value so
it is no longer a function
so it cant be called a second time.


(chaning should be changing)

Beverly
07-11-2011, 06:01 PM
Thanks for the feedback, that was a great help, I am just a beginner and have strugged for 3 days with this, and not found a solution or explaination. This at least makes logical sense.

I guess chaning means that after it returns the first form it moves on to being form[1] and therefore does not check the entry in my SLine1 input "text" field.

Do by any chance know how I might overcome this problem?

So I get it to keep checking until it finds a correct value in this box, and eventually all the other boxes I will have on this form?

I would appreciate the help.

devnull69
07-11-2011, 06:29 PM
It was changing and not chaning

Example:


function check() {
check=5;
}

"check" is the name of the function (which is an object in the global scope, too). It can be accessed using window.check. Now you assign a value (5 in my example) to the global object "check" thus you overwrite the function itself. It can not be called any more and window.check == 5

DaveyErwin
07-11-2011, 07:54 PM
Thanks for the feedback, that was a great help, I am just a beginner and have strugged for 3 days with this, and not found a solution or explaination. This at least makes logical sense.

I guess chaning means that after it returns the first form it moves on to being form[1] and therefore does not check the entry in my SLine1 input "text" field.

Do by any chance know how I might overcome this problem?

So I get it to keep checking until it finds a correct value in this box, and eventually all the other boxes I will have on this form?

I would appreciate the help.


<script type="text/javaScript">
function check() {
var check = document.forms[0].SLine1.value;
if(check == ""){
alert("please type something");
return false;}
else if (check != "correct") {
alert("please type correct");
return false;}
else{
return true;}
}
</script>



<body bgcolor="yellow">
<form method="GET" action="Distest_session30a_vs1" onSubmit="return check()">

<table>
<tr><td>
<input type="text" name="SLine1">
</td></tr>
</table>
<Input type="submit" value="Send now">
</form></body></html>

By using var the variable check
is no longer a global so no problem.

Beverly
07-12-2011, 08:22 AM
Thanks very much.

Changing check=document.forms[0].SLine1.value

to a non global variable with var

var check=document.forms[0].SLine1.value

worked a treat. Thanks very much did not realise doing it the way I had made the variable global and that this concept existing in javascript.

Thanks for all your help, getting me unstuck.

Beverly
07-12-2011, 08:23 AM
Thanks very much for your invaluable help.

And explainations.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum