I have a problem with the registry, I want to do to verify 9 digits mobile.
Code:
$tel = $_POST['tel'];
$sdt_length = strlen($tel);
$sdt_check = substr($tel, 0, 2);
elseif (!preg_match("/^[0-9]*$/i", $tel))
elseif ( ($sdt_check == '09' && $sdt_length == 10) || ($sdt_check == '01' && $sdt_length == 11) ) {
Phone formats can differ greatly from region to region.
I will assume that you are perfectly aware of the phone formats that you are dealing with and that they will not be POSTed with any kind of symbols.
You need a 9 digit string or a 10 or 11 digit string depending on the first two digits: 09 or 01, use this:
^(?:09\d{8}|01\d{9}|\d{9})$
See Demo
The above pattern will check for digit-only strings and length based on the first two digits.
PHP implementation:
if(isset($_POST['tel']) && preg_match("/^(?:09\d{8}|01\d{9}|\d{9})$/",$_POST['tel'])){
$tel=$_POST['tel'];
}else{
echo "Missing or Invalid Telephone Number";
}
Related
I am validating phone numbers and this is my conditions,
if (!empty($phone)) {
if (!filter_var($phone, FILTER_VALIDATE_INT) || !filter_var($phone, FILTER_VALIDATE_INT) === 0 || !is_numeric($phone) || !intval($phone)) {
// Error msg
// This segment working fine but
// Its throwing error msg when I am using a mobile number starting with a zero
// Like 01234567890
} else {
if (strlen($phone) > 16 || strlen($phone) < 8) {
// Error msg
} else {
// Valid msg
}
}
} else {
// Error msg
}
I want to through error msg if someone using 8 zeros or 16 zeros and I think its working but if someone using a valid phone number which is starting with a zero, then its throwing error msg as well.
How to pass number starting with a zero but mot all zeros?
Employ the same classic lookahead technique that is commonly used for validating passwords.
Ensure that the phone is between 8 and 16 digits and has at least one non-zero.
Code: (Demo)
echo preg_match('/^(?=0*[1-9])\d{8,16}$/', $phone) ? 'Pass' : 'Fail';
Do not cast phone numbers as integers or store them as integers in a database if they can possibly start with a zero in your project.
Without regex, use an assortment of string function calls for the same effect. Demo
echo ctype_digit(ltrim($phone, '0')) && $length >= 8 && $length <= 16 ? 'Pass' : 'Fail';
I am writing validation for a book management system, and I want to make it so that if the length of the ISBN entered is either less than or greater than 13 or 10, it displays the error message, however it says that there is an unexpected '<' whenever I try the following.
if(strlen($_POST['isbndelete'] (< 13 || 10) || (> 13 || 10)))
{
$error="The length of the ISBN is incorrect.";
echo $error;
return false;
}
All help is appreciated!
Your main issue is that your condition is not valid PHP. You should read more about conditional statements syntax.
Validing ISBN length
ISBNs are either 10 or 13 characters.
So you can simply check if your string does not contain exactly 10 and does not contain 13 characters either, like this:
$len = strlen($_POST['isbndelete']);
if ($len != 10 && $len != 13) {
$error = "The length of the ISBN is incorrect.";
echo $error;
return false;
}
I was searching for a regex which matches my requirement. But I couldn't find an exact one .
My requirement is
Add validation check to avoid Phone numbers with:
1) 6 digits equal (e.g. 000000 ; 111111)
2) sequence numbers (7 digits) (e.g.
1234567 ; 7654321)
I tried and got this piece of code finally
if (preg_match('/(\d)\1{5}/', $phone)) {
echo "Invalid Phone number";
}
But it matches only the first case. Hope some one will help me. Thanks in advance!
This is one of those times that I'd break away from regex.
This will perform your expected validation (and includes "around-the-clock" number sequences).
PHP Demo
$phone='000000';
$len=strlen($phone);
$rnd_the_clk='0123456789012345';
if(($len==6 && $phone==str_repeat($phone[0],6)) // length is 6, check only one integer used
||
($len==7 && (strpos($rnd_the_clk,$phone)!==false || strpos($rnd_the_clk,strrev($phone))!==false))){ // length is 7, check sequential
echo "invalid";
}else{
echo "valid";
}
Just learnt the basics of converting a Decimal Number to an Octal Number. Now for the reverse, I seem to have got the fact that any number whose last digit ending is either 8 or 9, cannot be an octal number.
But, is there anything else that I would need to check or do to see if an input number is an Octal Number or not (apart from checking 8 or 9 in the last digit)?. - [basically, enquiring if I am missing a certain process]
Below is my code in PHP:
<?php
$iOctal = 1423;
echo "What is the Decimal Value of the octal number $iOctal?"."<br/>";
$rg_Decimal = str_split($iOctal);
//print_r($rg_Decimal);
if (end($rg_Decimal) == 8 || end($rg_Decimal) == 9){
echo "<b>Error:- </b>Unable to process your request as the input number format is not of the Octal Number Format. Please try again...";
}
if ($iOctal < 8 && $iOctal >= 0){
echo "The Decimal Value of the octal number $iOctal is $iOctal.";
}
else{
$iE = count($rg_Decimal);
--$iE;
$iDecimal = 0;
for ($iA = 0; $iA < sizeof($rg_Decimal); ++$iA){
$iDecimal += $rg_Decimal[$iA] * bcpow(8,$iE);
--$iE;
}
echo "The Decimal Value of the octal number $iOctal is <b>$iDecimal</b>";
}
?>
It just so happened that during the testing, I had used an online resource. When I had given a particular number, it said that the number format was not octal. But the number did not have an 8 or 9 ending.
Looking forward to your kind support.
You can use the builtin function octdect($number) from php.
An example from http://php.net/manual/en/function.octdec.php , with the same question:
<?php
function is_octal($x) {
return decoct(octdec($x)) == $x;
}
echo is_octal(077); // true
echo is_octal(195); // false
?>
I'm trying to validate the length of a phone number staying within a range. Let's say at least 9 characters but not more than 12 so I can get international phone numbers.
I tried several things but none of them work.
The option below for instance validates correctly that it has not letter, however it doesn't matter the length of the number I introduce, I always get the Error Message: "Your phone number needs to have 9-11 numbers" even if I introduce a 9, 10 or 11 eleven digits number.
Thank you so much
if (empty($_POST["cellphone"])) {
$cellphoneErr = "Cell Phone is required";
} else {
$cellphone = test_input($_POST["cellphone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9]*$/",$cellphone)) {
$cellphoneErr = "Only numbers allow";
}
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
$cellphoneErr = "Your phone number needs to have 9-11 numbers";
}
}
Use preg_match() with quantifier {min,max}:
if (!preg_match("/^[0-9]{9,11}$/",$cellphone)) {
$cellphoneErr = "Has to be 9 to 11 numbers.";
}
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
Should be:
elseif(strlen($_POST["cellphone"]) < 9 || strlen($_POST["cellphone"]) > 11){
Your parenthesis are wrong.