2

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() ;)

1
  • i found they best way to do it with php is ctype_digit. some of the answers below allowed for .3 or -3 etc. Commented Apr 1, 2011 at 12:16

5 Answers 5

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Haven't tested it.

Comments

0

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.

Comments

0

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!

Comments

0

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() ;)

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.