I have created a home page with a division in which 2 other JSP pages are to load. The validations fire properly when I lauch those 2 pages seperately. The problem is, validations arent working when they load within the first page. How can I solve this? Should i write the script within the first page?


Home Page Code :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script>
            var zx;
                 function a()
                 {
                    zx="Login.jsp";
                    c(zx)
                 }
                 function b()
                 {
                     zx="Register.jsp";
                     c(zx)
                 }
                 function c(zx)
                 {
                        var xmlhttp;

                        if (window.XMLHttpRequest)
                                {// code for IE7+, Firefox, Chrome, Opera, Safari
                                    xmlhttp=new XMLHttpRequest();
                                }
                        else
                                {// code for IE6, IE5
                                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                                }
                        xmlhttp.onreadystatechange=function()
                            {
                                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                    {
                                    document.getElementById("a").innerHTML=xmlhttp.responseText;
                                    }
                            }
                        xmlhttp.open("GET",zx,true);
                        xmlhttp.send();
                 }


        </script>
        <link rel="stylesheet" type="text/css" href="projstyle.css" />
    </head>
    <body>
        <form method="post" action="/NewServlet">
            <font color="white">

            <br>
            <div id="a"><<input value="Login" type="button" name="btn1" onclick="a();">
                <input value="Register" name="btn2" type="button" onclick="b();">
                </div>
   </font>
        </form>
    </body>
</html>

two other forms :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <script type="text/javascript">
            function a()
            {

                        var x = document.forms.frm1.UserID.value;
                        var y = document.forms.frm1.Pass.value;

                    if(x=="" || y=="")
                        {
                            alert("Required Field cannot be left Blank");
                            if(x!="")
                                {
                                    document.forms.frm1.Pass.focus();
                                    return false;
                                }
                            else
                            {
                                document.forms.frm1.UserID.focus();
                                return false;
                            }
                        }
            }
            </script>
    </head>
    <body>
        <form method="post" action="/Login" onsubmit="return a()" name="frm1">
        Login : <input name="UserID" type="text"/>
        <br>
        Password : <input name="Pass" type="password" maxlength="18"/>
        <br>
        <input name="btn1" type="submit" value="Login!" />
        </form>

    </body>
</html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script language="javascript" type="text/javascript">
         function a(){
            var x = document.forms.frm1.Pass1.value;
            var y = document.forms.frm1.Pass2.value;
            var u = document.forms.frm1.txt1.value;
            var i = document.forms.frm1.txt2.value;
            var o = document.forms.frm1.sel1.value;

            if(x==null || x=="" || u==null || u=="" || i==null || i == "" || o=="Select One")
            {
                alert("Fields cannot be left blank");
                return false;
            }
            if(x!=y)
                {
                    alert("The Passwords do NOT match.");
                    return false;
                }
         }
        </script>
    </head>
    <body><font color="white">
        <form name="frm1" method="post" action="/Register" onsubmit="return a()"><table>
            <tr><td align="right">Name :</td><td><input name="txt1" type="text" /></td></tr>
             <br>
            <tr><td align="right">UserID :</td><td><input name="txt2" type="text" /></td></tr>
            <br>
            <tr><td align="right">Password :</td><td><input name="Pass1" type="password" /></td></tr>
            <br>
            <tr><td align="right">Confirm Password :</td><td><input name="Pass2" type="password" /></td></tr>
            <br>
            <tr><td align="right">City :</td><td><select name="sel1">
                                    <option value="Select One">Select One</option>
                                    <option value="Hyderabad">Hyderabad</option>
                                    <option value="Pune">Pune</option>
                                    <option value="Bangalore">Bangalore</option>
                                                </select>
                                        </td>
            <tr>
            <br>
            <tr><td align="right"><input value="Register" type="submit" /><td><td><input value="Cancel" type="submit" ></td></tr></table>
    </form></font>
        
    </body>
</html>

Function name crashing? You should give meaningful function names instead of a, b, c, etc. Then you will have the same function name when you try to include all pages together.

Well, I just changed the names. None of them clash now. But no luck at this yet.

OK, your validate function of both form may not return value correctly. You need to return either true or false on ALL conditions. You did only half for each.

// 1st form
function a2() {
  var x = document.forms.frm1.UserID.value;
  var y = document.forms.frm1.Pass.value;

  if(x=="" || y=="") {
    alert("Required Field cannot be left Blank");
    if(x!="") {
      document.forms.frm1.Pass.focus();
      return false;
    }
    else {
      document.forms.frm1.UserID.focus();
      return false;
    }
  }
  // If the script executes down to here, what it return???
}


// 2nd one
function a3() {
  var x = document.forms.frm1.Pass1.value;
  var y = document.forms.frm1.Pass2.value;
  var u = document.forms.frm1.txt1.value;
  var i = document.forms.frm1.txt2.value;
  var o = document.forms.frm1.sel1.value;

  if(x==null || x=="" || u==null || u=="" || i==null || i == "" || o=="Select One") {
    alert("Fields cannot be left blank");
    return false;
  }
  if(x!=y) {
    alert("The Passwords do NOT match.");
    return false;
  }
  // if the script executes down to here, what will it return???
}

Alright. I thought it would return true by default.

try it with jQuery completlety its better than using JS

JQuery is JavaScript... It is just a library which is actually pure JavaScript but make it simpler to be called by hiding most detail accessing work. Please do not suggest that using JQuery is better than JavaScript because they are the same. If you want to say, pure JavaScript is better than a general word.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.