javascript validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas1111
    New Member
    • Feb 2008
    • 122

    javascript validation

    Hi All..
    From 2 days i am learning javascript.. I have written a very basic code to accept a name and a submit boutton.. But my program is not running.. I am working in dreameweaver mx.. My problem is that when enter name and press submit boutton the name in the text box vanishes away.. and if i press submit button without entering any data no error message occurs .. can anyone help me to solve my problem pls ???
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    please show your code ...

    kind regards

    Comment

    • vikas1111
      New Member
      • Feb 2008
      • 122

      #3
      since i am not sure about my code so i am pasting all the lines without editing...

      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>test Document</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script language="JavaScript" type="text/javascript">
      <!--
        function validt()
         { 
          valid=true;
            if(document.form1.txtname.value=="")
               { alert("srtreter");
                  valid=false;
                 document.form1.txtname.focus();
               }
                    else{valid=true;}
                    return valid:
                }
             //-->
      </script> 
      </head>
      
      <body>
      <form name="form1" method="post" action="" onSubmit="return validt();">
      <p>Name 
          <input type="text" name="txtname">
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
      </p>
      </form>
      </body>
      </html>

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        try the following:

        [CODE=javascript]function validt() {
        var node = document.form1. txtname;
        var valid = node.value != '';

        if (!valid) {
        alert('srtreter ');
        node.focus();
        }

        return valid:
        }
        [/CODE]

        Comment

        • vikas1111
          New Member
          • Feb 2008
          • 122

          #5
          No its not working..No message appears when i press submit button without entering data into text box...

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            sorry i copied your typo:

            [CODE=javascript]return valid:[/CODE]
            MUST be:

            [CODE=javascript]return valid;[/CODE]
            of course.

            kind regards

            Comment

            • RamananKalirajan
              Contributor
              • Mar 2008
              • 608

              #7
              Originally posted by gits
              sorry i copied your typo:

              [CODE=javascript]return valid:[/CODE]
              MUST be:

              [CODE=javascript]return valid;[/CODE]
              of course.

              kind regards

              Code:
              <html>
              <head>
              <script language="javascript">
              function validThis()
              {
                var x = document.getElementById('textval');
                if(x.value=="")
                 {
                 alert("Missing");
                 return false;
                }
               return true;
              }
              </script>
              </head>
              <body>
              <input type="text" name="textval" id="textval"><br/>
              <input type="submit" value="submit" onclick="return validThis()">
              </body>
              </html>
              Hello Friend use this code it will be useful. In most cases please use the ID instead of names that will be very useful for you

              Regards
              Ramanan Kalirajan

              Comment

              • indirareddy
                New Member
                • Mar 2008
                • 14

                #8
                Hai This is Javascript Code


                1 <html>

                2. <head>

                3. <script language="javas cript">

                4. function check()

                5. {

                6. var reddy = document.getEle mentById('textv al').value;


                7. if(reddy.value= ="")

                8. {

                9. alert("Please provide some things");

                10. return false;

                11. }


                12. return true;


                13. }

                14. </script>


                15. </head>

                16. <body>

                17. <input type="text" name="textval" id="textval"><b r/>

                18. <input type="submit" value="submit" onClick="return check()">

                19. </body>

                20. </html>

                Comment

                • vikas1111
                  New Member
                  • Feb 2008
                  • 122

                  #9
                  Originally posted by indirareddy
                  Hai This is Javascript Code


                  1 <html>

                  2. <head>

                  3. <script language="javas cript">

                  4. function check()

                  5. {

                  6. var reddy = document.getEle mentById('textv al').value;


                  7. if(reddy.value= ="")

                  8. {

                  9. alert("Please provide some things");

                  10. return false;

                  11. }


                  12. return true;


                  13. }

                  14. </script>


                  15. </head>

                  16. <body>

                  17. <input type="text" name="textval" id="textval"><b r/>

                  18. <input type="submit" value="submit" onClick="return check()">

                  19. </body>

                  20. </html>


                  Hi still it isnt working

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    hi ...

                    just use the code i provided above and fix the return as i stated in post #6 ... it will work :)

                    kind regards

                    Comment

                    Working...