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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Mumbai
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking Validations

    I have made an login form and in that i m using JavaScript for checking validations. But the coding which i have done is just checking the validations for the first field ie User name & it is not checking whether the password field is left blank or not or it is not even checking other validations also.
    the coding goes here :-

    <html>
    <head>
    <title>The form</title>
    <SCRIPT LANGUAGE=JavaScript>
    <!--
    function validate()
    {
    if ( isName() && isPass())
    {
    document.login.submit();
    }
    return true;
    }
    function isName()
    {
    var str = document.login.firstname.value;
    if (str == "")
    {
    alert("\nThe Login Name field is blank .\n\nPlease re-enter your Login Name.")
    document.login.firstname.focus();
    return false;
    }
    }
    function isPass()
    {
    var str = document.login.passwd.value;
    if (str == "")
    {
    alert("\nThe PASSWORD field is empty")
    document.login.passwd.focus();
    return false;
    }
    if (str.length < 4)
    {
    alert("The PASSWORD field has less than 4 chars.\n Please re-enter your Password.")
    document.login.passwd.focus();
    return false;
    }

    }

    //-->
    </SCRIPT>
    </head>
    <BODY bgcolor="#5e9eff" background="C:\image001.jpg"><p>
    <center><br><br><br><br><br><br>
    <h2>Welcome to the Login Form</h2><br>
    <form name="login">
    <table border=14 bgcolor="#CCCCCC" >
    <tr><th>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <font color="red">
    Enter LoginName :
    </th>
    <td>
    <input type="text" maxLength=20 value="" name="firstname">
    </td></tr>
    <tr><th>
    &nbsp;&nbsp;
    <font color="red">
    Enter Password :</th>
    <td>
    <input type="password" value="" name="passwd" maxLength=12></td>
    </tr>
    <tr>
    <td colspan=2>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" value="Submit" name="submit" onClick="validate()">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="Reset" name="reset"></td></tr>
    <tr>
    <th colspan=2>
    <a href="email.html"><font size=2><face="times"><i>New User For Registration click here
    </font></a></th></tr>
    </form>
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Arl
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just add :

    <input type="button" value="Submit" name="submit" onClick="validate() && isPass()">

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Delaware, USA
    Posts
    137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this..

    <html>
    <head>
    <title>The form</title>
    <SCRIPT LANGUAGE=JavaScript>
    <!--
    function validate() {
    if ( isName() && isPass()) {
    return true;
    }
    return false;
    }
    function isName() {
    var str = document.login.firstname.value;
    if (str == "") {
    alert("\nThe Login Name field is blank .\n\nPlease re-enter your Login Name.")
    document.login.firstname.focus();
    return false;
    }
    return true;
    }

    function isPass() {
    var str = document.login.passwd.value;
    if (str == "") {
    alert("\nThe PASSWORD field is empty")
    document.login.passwd.focus();
    return false;
    }
    if (str.length < 4) {
    alert("The PASSWORD field has less than 4 chars.\n Please re-enter your Password.")
    document.login.passwd.focus();
    return false;
    }
    return true;
    }

    //-->
    </SCRIPT>
    </head>
    <BODY bgcolor="#5e9eff"><p>
    <center><br><br><br><br><br><br>
    <h2>Welcome to the Login Form</h2><br>
    <form name="login" onSubmit="validate()">
    <table border=14 bgcolor="#CCCCCC" >
    <tr><th>

    <font color="red">
    Enter LoginName :
    </th>
    <td>
    <input type="text" maxLength=20 value="" name="firstname">
    </td></tr>
    <tr><th>

    <font color="red">
    Enter Password :</th>
    <td>
    <input type="password" value="" name="passwd" maxLength=12></td>
    </tr>
    <tr>
    <td colspan=2>

    <input type="submit" value="Submit" name="submit" >

    <input type="reset" value="Reset" name="reset"></td></tr>
    <tr>
    <th colspan=2>
    <a href="email.html"><font size=2><face="times"><i>New User For Registration click here
    </font></a></th></tr>
    </form>
    </body>
    </html>

    ====================
    i put the validation onSubmit since they could possibly submit it with an 'enter' key. Also, you might want to change your background image. Not everyone has C:\image001.jpg on their systems The main thing is that you need to make sure each function returns something. I added a few return trues.


  •  

    Posting Permissions

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