Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy JavaScript only works once

    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:
    Code:
    <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?
    Last edited by Kor; 07-11-2011 at 01:06 PM. Reason: wrap the code [code][/code]

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    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)
    Last edited by DaveyErwin; 07-12-2011 at 12:47 PM. Reason: correct typo

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Do you know of a solution?

    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    It was changing and not chaning

    Example:
    Code:
    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

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by Beverly View Post
    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.
    Code:
    <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.

  • Users who have thanked DaveyErwin for this post:

    Beverly (07-12-2011)

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile resolved- using var for variable to check worked

    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.

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks very much for your invaluable help.

    And explainations.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •