-4

java-script single input separate into two. The first input is only 4 characters that called username and other password

<form action="a.php" method="post" id="myform" name="myform">
<label for="textfield">Prepaid Code</label>
<input type="text" name="prepaid" id="prepaid" />
<input type="hidden" name="user" id="user" value="document.getElementById('#prepaid').substring(0,4);"/>
<input type="hidden" name="pass" id="pass" value="document.getElementById('#prepaid').substring(4);"/>
<button onclick="document.getElementById("myform").submit()">SUBMIT</button>

How to make coding this by java script only

2 Answers2

0

I have to agree that what you are describing sounds like a bad idea. Whatever you are planning can probably be accomplished in a better way. Splitting a string in the way you described is accomplished easily with substring

let user = document.getElementById('#userpass').substring(0,4);
let password = document.getElementById('#userpass').substring(4);

EDIT: It looks like you just need PHP.

$user = substr($_POST['prepaid'], 0, 4);
$pass = substr($_POST['prepaid'], 4);
jonlink
  • 499
  • 5
  • 17
  • Sounds like you're looking for ajax. https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – jonlink Jan 29 '17 at 17:03
  • It looks to me like you don't need JS at all. From the *post result* part of your question, it looks like you're using PHP. Just use that. `$user = substr($_POST["user"], 0, 4);` – jonlink Jan 29 '17 at 18:30
0

i have got answer

<script type="text/javascript">


function vowcher(){
var prepaid = document.getElementById('prepaid').value;
var user1 = prepaid.substring(0,4);
var pass1 = prepaid.substring(4);

document.getElementById('user').value =user1;
document.getElementById('pass').value =pass1;
document.getElementById("myform").submit()
}
</script>


<form action="a.php" method="post" id="myform" name="myform">
<label for="textfield">Prepaid Code</label>
<input type="text" name="prepaid" id="prepaid" />
<input type="hidden" name="user" id="user" />
<input type="hidden" name="pass" id="pass" />
<button onclick="vowcher()">SUBMIT</button>
</form>