954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javascript Post action

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>XYZ Travels Pvt Ltd.</title>
        <script type="text/javascript"  language="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();
                    }
                 else{document.forms.frm1.UserID.focus();}
               }
            else
            {
               document.forms.frm1.action("Reg.jsp");
            }
        }

    </script>
    </head>
    <link href="projstyle.css" rel="stylesheet" type="text/css" />
    
    <body><font color="white">
            <form method="post" name="frm1">
        Login : <input name="UserID" type="text"/>
        
        Password : <input name="Pass" type="password" maxlength="18"/>
        
        <input name="btn1" type="button" onclick="a();"/>
        <a href="Reg.jsp">Register Now!</a>
    </form></font></body>
</html>

how do i cause the form to post to another page? Am I doing it correct? I want this to happen ONLY if validation passes.

alex8206
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Hi Alex,

Please check the sample below:

<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>
catherine sea
Junior Poster
126 posts since Jan 2008
Reputation Points: 25
Solved Threads: 20
 

Much more cleaner than my code. Thanx!

alex8206
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Glad it helps :)

catherine sea
Junior Poster
126 posts since Jan 2008
Reputation Points: 25
Solved Threads: 20
 

Nicely Explained

stanleyben
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: