简体   繁体   中英

Get input field value and display it in a div using javascript

I am trying to display input field value in a div after submitting the form.But I am unable to do it.
What I am doing wrong?.

can somebody help me?

 function validate() { var a = document.forms["Form"]["uname"].value; var validation=true; if ((a == null || a == "")||(!a.match(/^([a-zA-Z]{2,30})$/))) { document.getElementById('uname').className = 'box'; document.getElementById('popup').innerHTML= document.getElementById('uname').value; validation=false; } else{ document.getElementById('uname').className = ''; } return validation; } 
 .box { border: 2px solid red; } #popup { width:25%; height:115px; background-color: red; margin-top:2% ; margin-left: 60%; color: black; } 
 <head> </head> <body> <form method="post" name="Form" onsubmit="return validate()" action="" id="form_id"> <label>NAME:</label> <input type="text" name="uname" id="uname" class="fields" > <button type="submit" name="submit">SUBMIT</button> <div id="popup"></div> </form> </body> 

problem is if else condition. Inner html should be in else condition

 if ((a == null || a == "")||(!a.match(/^([a-zA-Z]{2,30})$/))) {
        document.getElementById('uname').className = 'box';
        validation=false;
      } else{
        document.getElementById('uname').className = '';
        document.getElementById('popup').innerHTML=  document.getElementById('uname').value;
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM