Reputation: 3
How do I add a space every three characters in a form in JavaScript and have a replace function? I've seen the question, but I'm still unclear and new at this and I'm not sure where the code would go into the script. This is what I have so far. Please be specific.
<script type="text/javascript">
function var myfunction()
{
}
</script>
</head>
<body>
<FORM ACTION="rules.html" NAME="form">
<INPUT TYPE=TEXT NAME="inp" SIZE=40 onkeyup="this.value=this.value.replace('t','A').replace('a','U')">
</FORM>
</script>
Upvotes: 0
Views: 1320
Reputation: 5316
It looks to me like your trying to enter in RNA and have it automatically split out into triplets on entry. But even if that's not what you're doing, this should get you what you need.
First, remove the extra closing </script>
tag you have below your form.
Then, replace the input
in your form with the following:
<input type="text" name="inp" size="40" onkeyup="this.value=myfunction(this.value);">
and in the myfunction
function use this:
function myfunction(val) {
val = val.replace('t','A').replace('a','U');
val = val.replace(/(\w{3})$/, '$1 ');
return val;
}
Granted this could all be cleaner if you pulled your javascript out of your markup and cleaned up your HTML a little, but this should get you going.
Upvotes: 1