www.webdeveloper.com
Results 1 to 3 of 3

Thread: Form Validation (one field)

  1. #1
    Join Date
    May 2010
    Posts
    36

    Form Validation (one field)

    Hi I need to validate the form, I am getting started by validating a simple username field. I have written the code but it does not seem to work for me. can anyone help:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
        <head>
            <meta name="generator" content="HTML Tidy, see www.w3.org">
            <meta http-equiv="Content-Type" content=
            "text/html; charset=iso-8859-1">
    
            <title>Registration Page</title>
    <script type="text/javascript">
                function validfrm()
                {
                    
                    if (document.frmregister.username.value == "") {
    					alert("Please enter your username");
    				}
                        return false;
                    
                    return true;
                    }
                
    </script>
        </head>
    
        <body>
            <form name="frmregister" method="post" action="#" onsubmit="return validfrm()">
            </form>
            <br>
            <br>
            <br>
    
            <table align="center" border="1">
                <tr>
                    <td>First Name</td>
    
                    <td><input type="text" name="username"></td>
                </tr>
    
                <tr>
                    <td>Last Name</td>
    
                    <td><input type="text" name="lastname"></td>
                </tr>
    
                <tr>
                    <td>Email ID</td>
    
                    <td><input type="text" name="emailid"></td>
                </tr>
    
                <tr>
                    <td>Password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Confirm password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Phone No.</td>
    
                    <td><input type="text"></td>
                </tr>
    
                <tr>
                    <td><input type="button" name="Submit" value=
                    "Register"></td>
                </tr>
            </table>
        </body>
    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Wrap the form tags around the table
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
        <head>
            <meta name="generator" content="HTML Tidy, see www.w3.org">
            <meta http-equiv="Content-Type" content=
            "text/html; charset=iso-8859-1">
    
            <title>Registration Page</title>
    <script type="text/javascript">
                function validfrm()
                {
                    
                    if (document.frmregister.username.value == "") {
    		    alert("Please enter your username");
                        return false;
    				}
                    return true;
                    }
                
    </script>
        </head>
    
        <body>
            <form name="frmregister" method="post" action="#" onsubmit="return validfrm()">
            <br>
            <br>
            <br>
    
            <table align="center" border="1">
                <tr>
                    <td>First Name</td>
    
                    <td><input type="text" name="username"></td>
                </tr>
    
                <tr>
                    <td>Last Name</td>
    
                    <td><input type="text" name="lastname"></td>
                </tr>
    
                <tr>
                    <td>Email ID</td>
    
                    <td><input type="text" name="emailid"></td>
                </tr>
    
                <tr>
                    <td>Password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Confirm password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Phone No.</td>
    
                    <td><input type="text"></td>
                </tr>
    
                <tr>
                    <td><input type="submit" name="Submit" value="Register"></td>
                </tr>
            </table>
            </form>
        </body>
    </html>
    http://www.yourhtmlsource.com/javasc...alidation.html

  3. #3
    Join Date
    May 2010
    Posts
    36
    Quote Originally Posted by Fang View Post
    Wrap the form tags around the table
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
        <head>
            <meta name="generator" content="HTML Tidy, see www.w3.org">
            <meta http-equiv="Content-Type" content=
            "text/html; charset=iso-8859-1">
    
            <title>Registration Page</title>
    <script type="text/javascript">
                function validfrm()
                {
                    
                    if (document.frmregister.username.value == "") {
    		    alert("Please enter your username");
                        return false;
    				}
                    return true;
                    }
                
    </script>
        </head>
    
        <body>
            <form name="frmregister" method="post" action="#" onsubmit="return validfrm()">
            <br>
            <br>
            <br>
    
            <table align="center" border="1">
                <tr>
                    <td>First Name</td>
    
                    <td><input type="text" name="username"></td>
                </tr>
    
                <tr>
                    <td>Last Name</td>
    
                    <td><input type="text" name="lastname"></td>
                </tr>
    
                <tr>
                    <td>Email ID</td>
    
                    <td><input type="text" name="emailid"></td>
                </tr>
    
                <tr>
                    <td>Password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Confirm password</td>
    
                    <td><input type="password"></td>
                </tr>
    
                <tr>
                    <td>Phone No.</td>
    
                    <td><input type="text"></td>
                </tr>
    
                <tr>
                    <td><input type="submit" name="Submit" value="Register"></td>
                </tr>
            </table>
            </form>
        </body>
    </html>
    http://www.yourhtmlsource.com/javasc...alidation.html
    Thanks; by the time my post got approved I had already figured it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles