...

View Full Version : Resolved Replace string in input field dynamically



javanew
09-20-2011, 03:47 AM
Hi, I am trying to write some code that will test if a user enters a non-numeric character in an input text field, and if so, replaces it with a '0'




<script type="text/javascript">
function IsNumVal (){
for (J=1; J<=3; J++){
if (isNaN(Number(document.getElementById('Intext' + J).value))){
document.getElementById('Intext' + J).value = 0;
}
}
}
</script>

<Body>
<table border="1" cellspacing="1" id = "Numbers">
<tr id="RowNum1">
<td ><input type="text" size = "5" name="Intext1" id="Intext1" value = "3"
onkeyup="IsNumVal()" /></td>
</tr>
<tr id="RowNum2">
<td><input type="text" size = "5" name="Intext2" id="Intext2" value = "9"
onkeyup="IsNumVal()" /></td>
</tr>
<tr id="RowNum3">
<td><input type="text" size = "5" name="Intext3" id="Intext3" value = "18"
onkeyup="IsNumVal()" /></td>
</tr>
</table>
</Body>


document.getElementById('Intext' + J).value = 0; doesn't work
document.Schedule.'Intext' + J.value = 0; doesn't work
document.Schedule.Intext2.value = 0; works, but I need to test each text field while looping, so I can't "hard code" the ID.

Suggestions?

xelawho
09-20-2011, 04:35 AM
<input type="text" id="Intext" onkeyup="value=IsNumVal(this.value)"></input>

function IsNumVal (str){
newstr=str.replace(/[^0-9]/g,0)
return newstr;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum