callum.bennett
callum.bennett

Reputation: 686

test if a form input is a 1 or 2 digit integer with jquery and php

I have a form that has five fields that are all set to maxlength="2".

Basically, i want the only values that can be entered to either be a one or two digit integer, because calculations are performed on these fields before the values are stored in the database.

Is there any jquery that will not let a user even enter a value that isnt an integer?

Also what would be the best way to validate this with both jquery and php? I have found some ways of doing it, but i need to make sure its secure, avoiding characters, -2, .1 etc


SOLVED

For the php part i used

if(!ctype_digit($_POST['value']))

which only allows for positive whole numbers

And for javascript

 $('.inputQuantity').keyup(function(){ 
    this.value = this.value.replace(/[^0-9\.]/g,'');
});

which deletes any entry made into the input field that is not a number.

Both above work, although i think some people prefer to use focusout(function() rather than keyup(function() ;)

Upvotes: 2

Views: 3958

Answers (5)

callum.bennett
callum.bennett

Reputation: 686

SOLVED

For the php part i used

if(!ctype_digit($_POST['value']))

which only allows for positive whole numbers

And for javascript

 $('.inputQuantity').keyup(function(){ 
    this.value = this.value.replace(/[^0-9\.]/g,'');
});

which deletes any entry made into the input field that is not a number.

Both above work, although i think some people prefer to use focusout(function() rather than keyup(function() ;)

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

Testing if a string contains only one digit or two digits, in PHP, can be done with a regex :

// $string is supposed to contain your data -- maybe obtained from $_POST['field_name']
if (preg_match('/^\d{1,2}$/', $string)) {
    // it's one or two digits
}

Basically, this tests for :

  • beginning of string : ^
  • A digit : \d
    • One or two times : \d{1,2}
  • End of string : $


And, in Javascript, I suppose you could use the same regex :

// str is supposed to contain your data
if (/^\d{1,2}$/.test(str)) {
    // it's one or two digits
}

Upvotes: 2

Jack Billy
Jack Billy

Reputation: 7211

I have created a PHP based function specially for you --

<form action="" method="POST">
       <input type="textbox" name="number" />
       <input type="submit" value="Check" />
</form>
<?php

  $i = $_POST['number'];

  function check_number_digits($i)
  {
      $func = preg_replace('/[^0-9]/', '', $i);
      if($_POST)
      {
          if(!$func)
          {
              echo 'You entered no Number!';
          }
          else
          {
              return strlen($func);
          }
      }
      else
      {
          //Can do anything!
      }
  }

  echo  check_number_digits($i);

?>

I have tested and it works perfectly! Tell me if it is helpful or not.

Hope it worked!

Upvotes: 0

mario
mario

Reputation: 145482

One theoretical newfangled way to ensure this is:

<input type="number" name="number1" maxsize="2" >

But you still need Javascript for older browsers:

<input name="num2" onchange="this.value = this.value.replace(/\D/, '')">

There is no way to avoid checking it server-side anyway.

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

$("input[maxlength=2]").change(function(){
   var val = $(this).val();
   $(this).val(parseInt(val));
});

Haven't tested it.

Upvotes: 0

Related Questions