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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript/html alert box help

    hi i am having an issue with javascript on my web site.
    i am a complete noob at this.

    the objective is to have the page pop up an alert any time the submit button is pressed:
    1st if there is no name on the form the alert says "please enter you first name" and returns a false value to the from
    2nd if there is a name on the form the alert is supposed to have a thank you message

    my problem is that it doesn't show the alert when there is a name

    here is the code

    Code:
    <head>
      <script type="text/javascript>
        function write(person) {
          alert("Thank you for conacting us, "+person+"!");
        }
                    
        function formgo() {
           var name=document.getElementById('namein').value;
           if (name==null || name=="") {
             alert("Please enter your name");
             return false;
           }
           writety(name);
           document.forms('contact').reset();
         }
      </script>
    </head>
    <body>
      <form>
        <fieldset id="contactinfo">
          <legend>Contact Details</legend>
          <label>
            Name
            <input type="text" name="name" id="namein" placeholder="First and Last name" />
          </label>
        </fieldset>
        <fieldset id="submitbox">
          <input type="submit" id="submit" value="Submit" onclick='formgo()' />
        </fieldset>
      </form>
    </body>
    please reply or email me

    thank you
    -misstam

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,188
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Change <input type="submit"...<input type="button" ...[/b]

    Might not be entire problem, but it's a start.
    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.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Also
    function write(person) {

    SS/B

    function writety(person) {

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    A good many problems, I am afraid.

    if (name==null || name=="") {

    A form field cannot be null, only "" (blank). For the so-manyth time, form validation of the pattern if (document.formname.formfield.value == "") 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 of course the user can enter something silly, such as ABC123. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    You should avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

    document.forms('contact').reset();
    should be
    document.forms[0].reset();
    You have no form or form element named "contact". And your form has no name nor id.

    Never use the name "submit" for a submit button, because then your JS code can't call document.formname.submit() if you ever need to. But in any case your submit button should be an ordinary button. A submit button does what it says on the tin - submits a form to a server-side script.

    In modern Javascript alerts should be used only for debugging - not to pass a message to the user.

    You say that this is your website, but you have no way of sending the form data to the server. So the user cannot "contact us".

    If you disclose your email address in a public forum you can expect to be deluged with spam.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 05-18-2012 at 08:51 AM. Reason: Noticed 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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Take no notice of iBall! He only repeats what others have already contributed in order to increase his post count. His reputation has now progressed beyond "iBall can only hope to improve" to "iBall is infamous around these parts". How true. I think he holds the world's record for the number of times he has been banned from this forum.

    There is a bird called an ostrich which thinks that if it puts its head in the sand so that it cannot see its enemies, then its enemies cannot see it!
    Last edited by Philip M; 05-18-2012 at 09:00 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.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Philip M View Post
    Take no notice of iBall! He only repeats what others have already contributed in order to increase his post count.
    I don't think I have seen a single post of his in the last month that hasn't simply echoed what was said in the prior post. Generally the suggested code is not to quite as high a standard though. Perhaps its time to start reporting such posts as contributing absolutely nothing to the thread since all they provide is a part of what the prior post provides anyway.

    The more knowledgeable members of this forum don't always agree but where they disagree they limit their comments to the advantages and disadvantages of their particular viewpoint which provides those reading the post with even more information (remembering that not only the original poster will read the responses but others with similar questions who do the right thing and search the forum before posting will as well).
    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.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by iBall View Post
    If my posts bother you, why not put me on your Ignore List?
    Because then I wouldn't be able to post corrections to your many mistakes and more people would end up believing that the antiquated solutions you provide are the right way to write their code now.
    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.

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    LOL

    (because I actually did)


  •  

    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
    •