Geoff
Geoff

Reputation: 197

JavaScript validation of numeric form field

I have been trying to validate a text field for numeric with JavaScript. I had it as a type "number" but IE 9 doesn't do this. This is the code I used:

var c=document.getElementById("employee_count").value;
if (c<=0)
  {
  alert("Employee numbers must be filled out");
  form1.employee_count.focus();
  return false;
  }
if (!is_int(c)) {
  alert("Employee numbers must be numeric");
  form1.employee_count.focus();
  return false;
} 

I first validated that there was some input and this worked but when I put in the code with !is_int and put an alphabetic character in to test, the whole form submitted without any further validation of fields. Can anyone spot what I am doing wrong? It is driving me crazy. If I omit the last if statement the subsequent code works correctly. Thanks in advance,

Geoff.

Upvotes: 0

Views: 391

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

That is JavaScript, not PHP. In JavaScript you could check like

if (isNaN(c)) {     //if c is not a number

Upvotes: 3

Related Questions