javascript - PHP validate integer is not functioning -
i'm trying make error box pop when theres letters inside of textbox, not working , i'm not sure need do.
here code section
<tr> <td align="right"> 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 website i'm not sure how post codes here therefore uploaded pastebin.
for complete code please click. http://pastebin.com/zfue6gwq
thanks in advance
suppose should modify validateinger function. first, take @ parseint specification. should remember return number if parameter not number. instance: parseint("12px") return 12. because of that, should better test value isnan (which return true in case if value "12px"):
function validateinteger(whichcontrol, controlname){ if(whichcontrol.value.length==0) return true; else if(isnan(whichcontrol.value)) alert(controlname + " must number!"); else return true; } but above code check if string number, not integer (like function name says). check if value integer, may next (using regexp):
function validateinteger(whichcontrol, controlname){ if(whichcontrol.value.length==0) return true; else if(!(/^[\d]+$/.test(whichcontrol.value))) alert(controlname + " must number!"); else return true; } (based on this answer)
note:
instead of onblur="validateinteger(diamonds.weight,'weight')"you may onblur="validateinteger(this,'weight')". this points element event happens.
Comments
Post a Comment