Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Tuesday, January 10, 2012

Allow Only Numbers (0-9) in Input Fields with jQuery

  $(".numbersOnly").keydown(function(e){
    var key = e.charCode || e.keyCode || 0;
    // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
    return (
        key == 8 || 
        key == 9 ||
        key == 46 ||
        (key >= 37 && key <= 40) ||
        (key >= 48 && key <= 57) ||
        (key >= 96 && key <= 105));
  });

Tuesday, August 3, 2010

Validating Integers without Regular Expression in PHP

ctype_digit checks if all of the characters in the provided string are numerical. Therefore, you must convert variables to string first, then you check them with ctype_digit.
$array = array(5, 5.2, "456", "564-223", "5.2", "673");
foreach($array as $item) {
   if(ctype_digit((string)$item)) {
      echo "int: ".$item."\n";
   } else {
      echo "not int: ".$item."\n";
   }
}