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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow Beginner: Why won't my script work??

    Please help me!! I am an absolute beginner with javascript. I can not seem to make this script work and am frustrated and not sure what i am doing wrong!! Can some kind soul help me? I want the script to check to make sure the user city is filled in and if not alert them. Should be simple, why can't I make it work?? AAARGHH
    <code>
    <script type='text/javascript'>
    function required()
    {
    var empt = document.forms["form"]["userCity"].value;
    if (empt == "")
    {
    alert("Please input a Value");
    return false;
    }
    }
    </script>

    </head>
    <body>
    <h1> JavaScript Form Handling</h1>
    <form name="jsform" method="post"
    action="http://webdevfoundations.net/scripts/formdemo.asp"
    onsubmit="return validateform();">
    <p>
    <label for= "userName">Name: </label>
    <input type="text" name="userName" id="userName"><br />
    <label for= "userAge">Age: &nbsp;&nbsp; </label>
    <input type="text" name="userAge" id="userAge"><br />
    <label for= "userCity">City:&nbsp;&nbsp; </label>
    <input type="text" name="userCity" id="userCity">
    </p>
    <p>
    <input type="submit" value="Send Information" id="submit" />
    </p>
    </form>
    </code>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    It would be easy to point out your error, but as the error console is doing it already, why ask?

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    It would be easy to point out your error, but as the error console is doing it already, why ask?
    What part of beginner did you not understand. I do not know what I am doing that is why I am in here asking. I thought someone might, just might be kind enough to help me. Obviously that someone is not you. Have a nice day.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Which browser are you using?

    In Firefox you can get to the error console by going into the "Tools" menu and then into the "Web Developer" sub-menu.

    It is in a similar location in other browsers.


    Also alert() is intended to be used only as a debugging tool. You shouldn't be using it in a live web page but including alerts between various statements and commenting out pieces of code are quick ways of locating problems.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    That really was the best possible answer for you, in the long run.

    You will never be able to get proficient in JavaScript if you don't learn to use the debugging tools--including the error console--that are built into almost all browsers.

    I would suggest that you use the Chrome browser, though MSIE 9 is actually quite usable for this as well. (FireFox doesn't include a full debugger; you have to install the FireBug plugin. Not hard to do, but if you are just starting out I'd just stick with Chrome.)

    To activate the debugger, including the error console, just push the F12 key on your keyboard. If you are indeed using Chrome, you will be presented with a list of "tabs" in the debug area at the bottom of the page. The rightmost tab--which will normally be the one that comes up automatically--is the error console.

    You should see a list of the errors on your page as detected by the JS engine. If you don't understand the error message for any one/all of them, just click on the thing on the right side of the tab that looks like an <a> link (it will have the name of the page and the line number with the error) and up will pop the JS code from your page with the point of the error highlighted.

    Fix the error and hit F5 (refresh page) and repeat the process until you get no more error messages.

    There is much more that the debugger console can do for you (you will notice all the other available tabs), so when you have a little time start exploring what is available.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    And I'll give you the first error for free:

    You coded
    Code:
    <form name="jsform" ... onsubmit="return validateform();">
    Where, pray tell, do you see any function named validateform in your code?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Mishelm View Post
    What part of beginner did you not understand. I do not know what I am doing that is why I am in here asking. I thought someone might, just might be kind enough to help me. Obviously that someone is not you. Have a nice day.
    I am sorry that you seem to have had unsympathetic and less than entirely helpful replies when you made it clear that you were an absolute beginner. It is certainly true that you ought to learn to use the debugging tools and error console, but it may be that is too advanced for you at this stage and your teacher has not yet got around to explaining it.

    Here is your code corrected:-

    Code:
    <script type='text/javascript'> 
    function validateform() {
    var empt = document.getElementById("userCity").value; 
    if (empt == "") { 
    alert ("Please input a Value"); 
    document.getElementById("userCity").focus();
    return false; 
    } 
    } 
    </script> 
    
    </head>
    <body>
    <h1> JavaScript Form Handling</h1>
    <form name="jsform" method="post"
    action="http://webdevfoundations.net/scripts/formdemo.asp" onsubmit="return validateform();">
    <p>
    <label for= "userName">Name: </label>
    <input type="text" name="userName" id="userName"><br />
    <label for= "userAge">Age: &nbsp;&nbsp; </label>
    <input type="text" name="userAge" id="userAge"><br />
    <label for= "userCity">City:&nbsp;&nbsp; </label>
    <input type="text" name="userCity" id="userCity">
    </p>
    <p>
    <input type="submit" value="Send Information" id="submit" />
    </p>
    </form>
    Two comments:-

    empt is not a very descriptive name for a variable which is supposed to contain the name of a city.

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. And the name of a city can only include letters (and perhaps hyphens), not numbers or punctuation. This topic has been covered many times before in this forum.

    As felgall says, you should not use alerts in a live web page.


    “The expert at anything was once a beginner”
    Last edited by Philip M; 11-20-2012 at 07:56 AM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Mishelm (11-20-2012)

  • #8
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Mishelm View Post
    What part of beginner did you not understand. I do not know what I am doing that is why I am in here asking.
    Forums start where searching ends.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Logic Ali View Post
    Forums start where searching ends.
    Can I ask - what is the question you expect the OP to enter into Google's search box?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I am sorry that you seem to have had unsympathetic and less than entirely helpful replies when you made it clear that you were an absolute beginner. It is certainly true that you ought to learn to use the debugging tools and error console, but it may be that is too advanced for you at this stage and your teacher has not yet got around to explaining it.

    Here is your code corrected:-

    As felgall says, you should not use alerts in a live web page.
    Thank you so much for being a decent human being.. you give me hope. I am a complete beginner and did not know where to start. I will install Chrome and try that going forward. I had searched and searched on google and could not understand. I think that some forget that they were once exactly where I am. I refused to feel shame for not knowing and trying to learn. Thank you again for taking the time to help... you made my day.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Mishelm View Post
    Thank you so much for being a decent human being.. you give me hope. I am a complete beginner and did not know where to start. I will install Chrome and try that going forward. I had searched and searched on google and could not understand. I think that some forget that they were once exactly where I am. I refused to feel shame for not knowing and trying to learn. Thank you again for taking the time to help... you made my day.
    That is very pleasing. Good luck!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Philip M View Post
    Can I ask - what is the question you expect the OP to enter into Google's search box?
    It's provided in the link I gave.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Logic Ali View Post
    It's provided in the link I gave.
    Hmm. Looking at the sites returned they seem singularly unhelpful. Especially for an absolute beginner.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Philip M View Post
    Hmm. Looking at the sites returned they seem singularly unhelpful. Especially for an absolute beginner.
    If they add to the query the name of their browser they must find something, that's if they haven't seen it at some time when going through the browser's menus. In any case for future occasions to avoid further criticism from those who expect to be spoon-fed, I have now drafted a detailed standard response. Here's its debut:

    All programming languages provide a means of reporting syntax and runtime errors. While it would be simple just to point you to the problem, your question makes it clear that you are not using your browser's Error Console, which must be indicating the nature of at least one error.
    If you are studying solely from written materials, they should have made you aware of the existence and use of the The Error Console. If you have any form of tutor, he/she should have instructed and disciplined you in the use of The Error Console during your first lesson/lecture. If the latter is the case, it is your duty both to yourself and future students to take your tutors to task on the matter, so that the oversight is never repeated. If your tutors disclaim any such obligation, you are encouraged to refer them to this or any similar forum, on which they may explain their rationale.
    Searching for how to access your particular browser's error console/developer tools is a simple matter and something you must do immediately. By so doing you will save yourself incalculable time, frustration and the humiliation of requesting unnecessary assistance.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Logic Ali View Post
    If they add to the query the name of their browser they must find something, that's if they haven't seen it at some time when going through the browser's menus. In any case for future occasions to avoid further criticism from those who expect to be spoon-fed, I have now drafted a detailed standard response. Here's its debut:
    Does that not sound rather ... pompous?

    It is really just your version of "Read The Flipping Manual". May be strictly correct, but seems to me unnecessarily supercilious and patronising.
    You should bear in mind that you will catch more flies with honey than with vinegar.
    Last edited by Philip M; 11-21-2012 at 07:36 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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