Validation for numeric values

Hi everybody,

I have a problem in asp application validation. My textbox only accepts numeric value…
It should not accept empty field and negative values and charcters…

I got it for empty field…
but not for numeric value…

function providerid(fld){
  var error=" "
   if(fld.value== ""){
  fld.style.background='Yellow';
  error= "You didn't enter a product id \
";
  }else{
    fld.style.background='White';
  }return error;
}
	
function validateFormOnSubmit(form1) {
  reason += providerid(form1.pid);
  if (reason != "") {
    alert("fields neccessary:\
" + reason);
   return false;
   }
   return true;
}	

I am not aware of IsNan function…
Can anyone give some idea about numeric validation…

isNaN (is not a number)

console.log (isNaN('abc')); // true

!isNaN -  (!)Not not a number. In other words 'is a number'
console.log (!isNaN('12')); // true
console.log (!isNaN('-12')); // true, which you don't want.

Using a regular expression to test if anything other than 0 to 9 or a dot(decimal place) are present.

var isNum = function(num){return !/[^.[0-9]]*/.test(num);};
console.log(isNum('12.12')); // true
console.log(isNum('5')); // true
console.log(isNum('abc')); // false
console.log(isNum('-12')); // false

Why not just use the Number object?


If (Number(fld.value) > 0) {
  // is valid
} else {
 // not valid
}

Empty strings will be cast to 0 with the above code, so if you also need 0 to be accepted:


If (fld.value === '0' || Number(fld.value) > 0) {
  // is valid
} else {
 // not valid
}

Why not just use the Number object?]

Fair enough. I tend to miss the simple solutions.

Had to read up on the number object and I see that it converts it’s argument to a numeric value or returns NaN if it fails.

Unless I’m missing something ‘!isNaN(fld.value) && fld.value >= 0’ would also do the trick.

or

if (Number(fld.value) >= 0)

edit: both ‘if (Number(fld.value) >= 0)’ and ‘!isNaN(fld.value) && fld.value >= 0’ fail due to an Empty string returning 0.

Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?

The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.

If you don’t intend to allow 0, you can just check that the number is greater than 0 and you’re set.

Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?

In answer I don’t know. Haven’t really used isNaN that much.

Given that Number returns NaN when it fails, I just thought I’d experiment.

Number is obviously a simpler and clearer solution.

The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.

Yep missed that.

Still a lot to learn.

Thanks, so it seems that after we’ve considered various issues, that we’re left with something like this:


If (fld.value === '0' || Number(fld.value) > 0) {
  // is valid
} else {
 // not valid
}

or


If (Number(fld.value) > 0) {
  // is valid
} else {
 // not valid
}

Depending on if you want 0 to be allowed or not.

Thanks for all your answers…

Finally I am confused what i have to use for numeric validation…
is there a way other than isNan()…

Yes there is. If you want to ensure that it’s a valid number greater than 0, you can use:


if (Number(fld.value) > 0) {
  // is valid
} else {
 // not valid
}