0

i want a text box that does not accept letters, negative numbers and sign. just accept positive numbers.

if user inputs a value like $%^# or words like a...z or numbers like this -455, it should give an error I want to do this with PHP, not Jquery.

sammy
  • 717
  • 4
  • 13

1 Answers1

0

PHP is a server side scripting language, so you can't use it to prevent users entering letters or symbols into that field. What you can use it for is to validate the contents of the field once the user has submitted the form, and then display an error or something if the value they have submitted does not conform to your standard.

I'm assuming that this field is part of a form that is being submitted via a POST request. If so, you can use PHP's filter_input function to validate the contents of the POSTed value that corresponds to the text field you want to validate. Use the FILTER_VALIDATE_INT filter to ensure that the value is an integer, and the min_range option to ensure that it is positive - like this:

$options = array('options' => array('min_range'=>0))
$number = filter_input(INPUT_POST, 'yourfield', FILTER_VALIDATE_INT, $options);

Your $number variable will then be FALSE if the POSTed field yourfield is not a positive integer, or the positive integer otherwise.

James K
  • 89
  • 1
  • 10