How to display form input on the same page using Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • korupolu
    New Member
    • Jan 2011
    • 3

    How to display form input on the same page using Javascript?

    I would like to display the details of all the Input fileds entered by user back in the same page. is it possible? if so, i would also like to alert user not to keep the field empty.
  • mad genius
    New Member
    • Jan 2011
    • 3

    #2
    I'm not a 100% sure what you want but I think this is what you want:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function formcheck(){
    	var blankfields = new Array()
    	if(document.getElementById("country").value == "Country"||document.getElementById("country").value == ""){
    		blankfields.push("Country")
    	}
    	if(document.getElementById("email").value == "Email"||document.getElementById("email").value == ""){
    		blankfields.push("Email")
    	}
    	if(blankfields == ""){
    		alert("Thank You For Filling Out Our Survey!")
    	}
    	else{
    		blankfieldsjn = blankfields.join("\n")
    		alert("You need to fill out the following fields before proceeding:" + "\n" + blankfieldsjn)
    	}
    }
    </script>
    </head>
    <body>
    <form>
    <input id="country" type=text value="Country"><br />
    <input id="email" type=text value="Email"><br />
    <input type=button value="Submit" onClick=formcheck()>
    </form
    </body>
    </html>

    Comment

    • korupolu
      New Member
      • Jan 2011
      • 3

      #3
      Dear Mad Genius,

      Below is my code. I would like to alert the user not to keep any field blank and the cursor should point to the field where the user is trying to skip the field when he presses enter key or submit key.

      Code:
      <HTML>
        <HEAD>
          <TITLE>Basic Details</TITLE>
      
          <script type="text/javascript">
            function details()
            {
              var name1 = document.myform.name.value;
      	if (name1.length === 0) {
      	alert("You must enter your Name ");
      	return false;
      	 }
      		
      	var age1 = document.myform.age.value;
      	if (age1.length === 0) {
      	alert("You must enter your Age ");
      	return false;
      	 }
      
      	var city1 = document.myform.city.value;	
              if (city1.length === 0) {
      	alert("You must enter your City ");
      	return false;
      	 }
      
      	document.getElementById("divid1").innerHTML = name1;
      	document.getElementById("divid2").innerHTML = age1;
      	document.getElementById("divid3").innerHTML = city1;
      
      	return true;
      	
      	}
          </script>
      
        </HEAD>
      
        <BODY>
      
          <FORM NAME="myform">
      Enter your name: <BR>
            <INPUT TYPE="text" NAME="name" VALUE="" onClick="details();"><BR>
      Enter your age: <BR>
            <INPUT TYPE="text" NAME="age" VALUE="" onClick="details();"><BR>
      Enter your city:<BR>
            <INPUT TYPE="text" NAME="city" VALUE="" onClick="details();"><P> 
            <INPUT TYPE="button" Value="Submit" onClick="details()">
      	<INPUT TYPE="reset" VALUE="Reset">
          </FORM>
      
      You have entered the following details:
      <P>
      Your Name is: <span id="divid1"></span><P>
      Your Age is: <span id="divid2"></span><P>
      Your City is: <span id="divid3"></span>
      
      </BODY>
      </HTML>
      Last edited by Dormilich; Jan 27 '11, 06:33 PM. Reason: please use [CODE] [/CODE] tags when posting code

      Comment

      Working...