SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with text validation using href urgent

    hello everyone,im having some problems here.I want to make a javascript program using href, i made a sample using a button but i cant turn it to a href, can anyone help me?

    heres my sample code

    JAVASCRIPT CODE placed in the <head> tag
    Code JavaScript:
      <SCRIPT LANGUAGE="JavaScript">
     
      function everything(form) 
            { blank(form) }
     
      function blank(form) 
            { 
              if((Username(form) && Position(form))) 
                { form.submit() }
              if((Username(form) == false  || Position(form) == false)) 
                { compose(form) }
            }
     
      function compose(form)   
            {
              var text = "Field/s required:"	
              if(Username(form) == false) 
                { text += "\nUser Name" }
     
              if (Position(form) == false) 
                { text += "\nPosition" }   
     
                alert(text)
            }
     
      function Username(form) 
            {
              if (form.Uname.value == "") 
                { return false }
              else 
                { return true }
            }
     
      function Position(form) 
            {
              if(form.Position.value == "") 
                { return false }
              else 
                { return true }
            }
     
    </SCRIPT>


    here is my html code

    Code HTML4Strict:
      <form method="GET" action="test2.html">
    <table>
      <tr>		
          <td> Username: *</td>
          <td> <input type="text" value="" name="Uname" size=20></td>
      </tr>
     
      <tr>
          <td> Position: *</td>
          <td>
            <SELECT name="Position">
     	<OPTION> &nbsp;
     	<OPTION value=test1>test 1
     	<OPTION value=test2>test 2
            </SELECT>
          </td>
      </tr>
     
      <tr>
          <td align="center" colspan="2" >
          <input type="button" name="thesubmit" value="Submit" onClick="everything(this.form)" >
     
      <a href="test2.html" onClick="everything(this.form)">Save</a>
        <input type="reset" value="Reset">
           </td>
     </tr>
    </table>
      </form>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Code:
    <button type="submit" name="thesubmit" value="Submit" onClick="return everything(this.form)" >Submit</button>
    also your select menu is a bit wrong:
    Code:
    <SELECT name="Position">
        <OPTION> &nbsp;</OPTION>
        <OPTION value=test1>test 1</OPTION>
        <OPTION value=test2>test 2</OPTION>
     </SELECT>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhmm wat i want is that the href is the one calling the javascript functions,is that possible? the program i made calls the function using the <input type=button> what i want is that the href to call it...

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Give your form a name for referencing.
    Code:
    <form method="GET" action="test2.html" name="sendForm">
    <table>
      <tr>   
          <td> Username: *</td>
          <td> <input type="text" value="" name="Uname" size=20></td>
      </tr>
           
      <tr>
          <td> Position: *</td>
          <td>
            <SELECT name="Position">
        <OPTION> &nbsp;</OPTION>
        <OPTION value=test1>test 1</OPTION>
        <OPTION value=test2>test 2</OPTION>
            </SELECT>
          </td>
      </tr>
     
      <tr>
          <td align="center" colspan="2" >
          <input type="button" name="thesubmit" value="Submit" onClick="everything(this.form)" >
     
      <a href="#" onClick="return everything(sendForm); document.sendForm.submit();">Save</a>
        <input type="reset" value="Reset">
           </td>
     </tr>
    </table>
      </form>
    Should work
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried it,uhmm it does call the function but it still continue to the linked page...what i need to do is that when the textbox is empty it will stay on the same page...tnx for the answer though

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You have changed the href to a # haven't you?
    Works for me in FF and IE.

    Post the code as you have it if it still doesnt work
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here the whole code of my page

    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title>Test Page</title>
     
      <SCRIPT LANGUAGE="JavaScript">
     
      function everything(form) 
            { blank(form) }
     
      function blank(form) 
            { 
              if((Username(form) && Position(form))) 
                { form.submit() }
              if((Username(form) == false  || Position(form) == false)) 
                { compose(form) }
            }
     
      function compose(form)   
            {
              var text = "Field/s required:"	
              if(Username(form) == false) 
                { text += "\nUser Name" }
     
              if (Position(form) == false) 
                { text += "\nPosition" }   
     
                alert(text)
            }
     
      function Username(form) 
            {
              if (form.Uname.value == "") 
                { return false }
              else 
                { return true }
            }
     
      function Position(form) 
            {
              if(form.Position.value == "") 
                { return false }
              else 
                { return true }
            }
     
    </SCRIPT>
      </head>
      <body>
    <form method="GET" action="test2.html" name="sendForm">
    <table>
      <tr>   
          <td> Username: *</td>
          <td> <input type="text" value="" name="Uname" size=20></td>
      </tr>
     
      <tr>
          <td> Position: *</td>
          <td>
            <SELECT name="Position">
        <OPTION> &nbsp;</OPTION>
        <OPTION value=test1>test 1</OPTION>
        <OPTION value=test2>test 2</OPTION>
            </SELECT>
          </td>
      </tr>
     
      <tr>
          <td align="center" colspan="2" >
          <input type="button" name="thesubmit" value="Submit" onClick="everything(this.form)" >
     
      <a href="test2.html" onClick="return everything(sendForm); document.sendForm.submit();">Save</a>
        <input type="reset" value="Reset">
           </td>
     </tr>
    </table>
      </form>
      </body>
    </html>

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    As I said, take test2.html out of the link!
    That will take priority over the form submit.
    Code:
    <a href="#" onClick="return everything(sendForm); document.sendForm.submit();">Save</a>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok sorry about that,but is it possible to link it to another page?

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    The form will go wherever you have asked it to in the action
    Code:
    <form method="GET" action="test2.html" name="sendForm">
    change the action to direct it to your page
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok tnx


Bookmarks

Posting Permissions

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