0

I'm trying to make an error box pop up when theres letters inside of the textbox, however it is not working and I'm not sure what I need to do.

Here is the code for the section

  <tr>
    <td align="right">&nbsp;&nbsp;Weight:</td>
    <td><input type="text" name="weight"  value="<?php echo $weight?>" onblur="validateinteger(Diamonds.weight,'weight')" <?php if($formmode==1) echo 'disabled';?> size="15" /></td>
 </tr>

I'm new to this website so I'm not sure how to post all the codes here therefore I uploaded everything to pastebin.

For complete code please click. http://pastebin.com/ZfUe6GWq

Thanks in advance

Dan Higham
  • 3,974
  • 16
  • 15

1 Answers1

0

Suppose you should modify your validateinger function. First, take a look at parseInt specification. You should remember that it will return a number even if parameter is not completely a number. For instance: parseInt("12px") will return 12. Because of that, you should better simply test value with isNaN (which will return true in case if value is "12px"):

function validateinteger(whichcontrol, controlname){
   if(whichcontrol.value.length==0)
     return true;
   else if(isNaN(whichcontrol.value))
     alert(controlname + " must be a number!"); 
   else
     return true;
  }

But above code will only check if string is a number, not integer (like function name says). To check if value is exactly integer, you may do next (using regexp):

function validateinteger(whichcontrol, controlname){
   if(whichcontrol.value.length==0)
     return true;
   else if(!(/^[\d]+$/.test(whichcontrol.value)​))
     alert(controlname + " must be a number!"); 
   else
     return true;
}

(based on this answer)

Note:

Instead of onblur="validateinteger(Diamonds.weight,'weight')"you may do onblur="validateinteger(this,'weight')". Where this points to an element where event happens.

Community
  • 1
  • 1
Viktor S.
  • 12,736
  • 1
  • 27
  • 52