...

View Full Version : check for integers



scriptkeeper
07-14-2003, 02:10 AM
I have this script im working on, an I want it to searches a form and removes any characters that are not an integer but for some reason I am unable to do this?


<html>
<head>
<script>
var check_string="";
var ending_string="";
function check_char(){
check_string=document.entry.data.value;
for(i=0;i<check_string.length;i++){
if(!isNaN(check_string.charAt(i))){
ending_string+=check_string.charAt(i);
}
}
check_string="";
check_string=ending_string;
ending_string="";
return false;
}


</script>
</head>
<body>
<form name="entry" onSubmit="return check_char()">
<input type="text" name="data" value=""/>
<input type="submit" value="check"/>
</form>
</body>
</html>

Philip M
07-14-2003, 07:54 AM
You are going about this the hard way. A regular expression will solve your problem:-

<input type="text" name="data" onblur="if(/\D/g.test(this.value)){alert('Only numbers are allowed in this box. '); this.value=''; this.focus()}">

ellisd5
07-14-2003, 11:08 AM
Yea reg expressions are the way to go, I've been trying to get to grips with them lately.

Instead of alerting that its not a number you can automcaticly strip non numerics



<input type="text" name="data" onblur='this.value=this.value.replace(/[^0-9]/g, "");'>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum