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

4
  • 1
    This is a javascript issue not a php one Commented May 13, 2013 at 7:58
  • Check console in your browser (usually could be opened with F12, may need to install Firebug in FireFox). See if there are some errors. Commented May 13, 2013 at 7:59
  • What is not working? What do you get? Commented May 13, 2013 at 8:10
  • @nl-x nothing at all, it just allows the letter to sit there without showing any error msg Commented May 13, 2013 at 8:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.