...

View Full Version : number check



docock
02-26-2007, 11:04 AM
NEVER MIND, problem is solved

Hi there, I want to check if a field is number only.. I've tried with the code below..though when i fill in something like '33q' it doesn't display the alert..
what am i doing wrong ?

<html>


<script language="JavaScript" type="text/javascript" defer="defer">

function checkhuis(eleID){
eleObj = document.forms[0].elements[eleID];
regex = /^[0-9]/;

if ( eleObj.value.length !=0 ){
if ( regex.test(eleObj.value) == false) {
alert('please adjust your number');
eleObj.focus();
}

}
}

</script>


<form name="formpje">
Enter a number then click the button:
<input type=text name="input" id="test" size=10 value="" onblur="checkhuis(this.id)">



<br><br>


</form>
</html>

BarrMan
02-26-2007, 12:32 PM
Try parsing it to int - parseInt and then check if the value is NaN.

docock
02-26-2007, 01:22 PM
ive tried that without any succes :S
Isn't there a way to use a normal regexp ?

BarrMan
02-26-2007, 01:27 PM
function checkhuis()
{
var obj = document.forms[0].input;
if(obj.value == "NaN")
alert("Not a number!");
}

Philip M
02-26-2007, 01:58 PM
function checkhuis()
{
var obj = document.forms[0].input;
if(obj.value == "NaN")
alert("Not a number!");
}

The NaN value cannot be tested for with the == equality operator.
Correct syntax is:

if (isNaN (obj.value)) {

BarrMan
02-26-2007, 02:01 PM
Oh, sorry, my mistake.

rwedge
02-26-2007, 10:54 PM
I like this hack:

<script type="text/javascript">
<!--
function checkhuis(n) {
if ((n != 0)&&(n/n != 1)) alert('Numbers Only');
}
// -->
</script>

<input type=text name="input" id="test" size=10 value="" onblur="checkhuis(this.value)">

brandonH
02-27-2007, 03:34 AM
if(isNaN(n)){alert('numbers only');return false;}
else{return true;}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum