Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox validation - only integers 1

Status
Not open for further replies.

jkris

Programmer
Jun 12, 2003
13
0
0
IN
is there any shortcut code 4 checking whether the text field contains only numbers(integers, shudn't contain negative values nor decimals)


regards,
JK
 

function numeric_only()
{
if ((event.keyCode < 48) || (event.keyCode > 57)) {
event.keyCode = 0;
}
}
 
<input type=&quot;text&quot; name=&quot;textfieldName&quot; size=&quot;24&quot; border=&quot;0&quot; onBlur=&quot;findNum(this.value);&quot;>

function findNum(m)
{
var i = 0;
var tempStr = m;
var lgn = tempStr.length;

for(i = 0; i < lgn; i++)
{
if((tempStr.charAt(i) > -1) && (tempStr.charAt(i) < 10))
{
continue;
}
else
{
window.alert(&quot;Is not a whole number&quot;);
break;
}

}

}

~mgb
 
<input type=&quot;text&quot; name=&quot;numfield&quot; onBlur=&quot;checkInt(this);&quot;>

<script>
function checkInt(elem)
{
elem.value=elem.value.replace(/\s/g,''); //get rid of any blank spaces before checking.
if(!/^\d*$/gi.test(elem.value)){
alert(&quot;not an Integer number&quot;);
elem.select();
elem.focus();
}
}
</script>


grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top