php ALLOW only a range of numbers from a FORM field
php ALLOW only a range of numbers from a FORM field

Today in our project we code to make php ALLOW only a range of numbers from a FORM field not to be inserted into the database.

Shopinson actually is going to be validating numbers in two ways for his target aim which will be for PHONE numbers and the later the Common Integers ranging between fourteen to sixteen DIGITS  and zero to ten respectively using PHP for form fields.

This practical we will be using php operands

Though we have discussed this earlier in our previous post of validating a text to ALLOW a range of number using javascript and jquery validating only in such manner is not globally accepted because someone may bypass the javascript / jquery validation but will be trapped if the php allow the range of number function is enabled.

strlen($phone) < 14 or strlen($phone) > 16

this post validates that if the string length of php variable $phone is less than 14 or greater than 16 returns the result.

if(strlen($phone) < 14 or strlen($phone) > 16) { print"result"; }

WHY this

This post validation is going to describe the php strlen to validate the digits of numbers to be accepted in database through the html form

i want to validate a phone so the input will not exceed 16 digits and not less than 14 digits so I applied the above codes

this code is a perfect fit best code snippet of php to allow between 14 to 16 digits of phone numbers while validating phone numbers

$phone = "0811222323232";
if(strlen($phone) < 14 or strlen($phone) > 16) { print"invalid phone number".strlen($phone); }else print"valid phone number".strlen($phone);

the php script will return invalid phone number13  because the phone (MOBILE)  is less than 14 digits but will return valid phone number14 if a single character is added to the value of the php variable $phone see for the example below

<?php  
$phone = "0811222323232+";
if(strlen($phone) < 14 or strlen($phone) > 16) { print"invalid phone number".strlen($phone); }else print"valid phone number".strlen($phone);
?>

notice that even if the extra digits number or variable character is added it will still return valid Hey friend! to make sure to validate field inputs are all integers number so php will be validating phone numbers only use the below code

<?php  
$phone = preg_replace('/[^0-9]/', '', "0811222323232+");
if(strlen($phone) < 14 or strlen($phone) > 16) { print"invalid phone number".strlen($phone); }else print"valid phone number".strlen($phone);
?>

Php validate mobile phone number

we introduced preg_replace('/[^0-9]/', '', "0811222323232+") to validate the phone completely you will not is will return invalid phone number13

this preg_replace('/[^0-9]/', '',"$string")actually perform a php magic it checks if the input value of a html form field are numbers if they are then it replaces any special null value and return the results therefore its work differes from the actually is_numeric of php that Finds whether a variable is a number or a numeric string

PHP to allow only numbers less than 10 in form field but greater than 0

Good, now php ALLOW a range of number is only possible not with the above illustration but we showed you the above illustration to introduce to what code you might be writing instead of writing exactly the one below

<?php  
$x = 11;
if($x < 0 or $x > 10){
  print"invalid range is " .$x;
}else{
  print"valid range is " .$x;
}
?>

this code will output invalid range is 11 because it exceeds the defined number range 0 – 10

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here