How to check if text has 6 numbers - php

I have a jobs site , and my visitors are able to add jobs on the front end of it , i need to check the job details to see if it has a phone number within it or not using php code
example of the 6 digit numbers could be 123456 , 533434 and no other format,
if there is any 6 digit numbers an error message would show up to make visitors remove it from the details section
I need something like this :
if (strpos($_POST[details],'######') !== false)
{
echo 'remove the phone number from job details';
}

if (preg_match('/[1-9]\d{5}/',$_POST[details])) {
echo 'remove the phone number from job details';
}

You have to use a regular expression like this: \d{6}
if (preg_match('#\d{6}#', trim($_POST['details']))) {
// Then the phone number is valid
}

Related

Invalid if Special Character is entered

I currently have this code below which validates username length only. If none entered it will show error message, if less than 3 characters entered show error message. I want to add an if/else statement that if the user enters special characters like !##$%^&*()+=/? etc... the only special character is allowed is underscore (_) and hypen (-)... Help me how.
thanks
here's the code i have:
<?php
$serror="";
if (isset($_POST['submit'])) {
$username=$_POST['username'];
$lengt = strlen($username);
if($lengt == 0){
$serror=" Please enter account username ";
}
else{
if($lengt < 3 ){
$serror=" Please enter valid account username ";
}
}
if($serror==""){
ob_start();
echo "Success";
header("Location:progress.php?username=$username");
exit;
ob_end_flush();
}
else{}
}
?>
use preg_match() function
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
remeber preg_match() returns boolean
Your php script works after the user submits the form. From your tags in the question I assume you can use javascript. With Javascript you catch these errors before the form is submitted.
So, your html input field would fire a script and you can use the onkeypress event to see the value of the keystroke.
The submit button would also have a javascript event to look for min string length, else give warning and not submit form.
As others already pointed out, you should use regular expressions for this.
Try with the following if-statement (allows a-z, numbers, underscores and hyphens). It also checks that the length is at least 3 characters:
if (!preg_match("/^([\w\-]{3,})$/", $username)) {
$error = "Not enough chars or there are invalid ones".
}
Read more about preg_match() here

How Mobile number validation varies with the URL?

I have written mobile number validation script as you can see in below.the problem it is working fine with the URL1.but not with the URL2.I want to know why is that?
URL1 - http://axbc.com/con/s2.php
URL2 - http://axbc.com/con/s2.php?key=d908e8401774411861043
Here is my Code
if(isset($_POST['btn-signup']))
{
$mobilenumber = $_POST['mobilenumber'];
if(!empty($dialogmobilenumber)) // phone number is not empty
{
if(preg_match('/^947\d{8}$/',$mobilenumber)) // phone number is valid
{
echo 'success';
}
else // phone number is not valid
{
echo "<script>alert('Mobile Number is Not valid.. Format should be ');
</script>";
}
}
}
Your code is working fine for me in both urls.
It is doing, what it is meant for, It is matching all the 11 digit numbers starting from '947'+8 nos.
As, you are not using the passed variable anywhere in this piece of code,So it can never be the cause of your error.
One thing,You are receiving mobilenumber as POST in $mobilenumber variable.
So,why are you using $dialogmobilenumber to check if it is empty or not ?
Have you defined this anywhere in the program.
if(!empty($dialogmobilenumber)) // phone number is not empty
{ ...
Another thing, Is your form also present in s2.php ?
If not, what is your action contains (action="?") for the form ?
Note: If you can paste the complete code and output that you are getting for
URL2, that will be good.

How to detect if an element is visible using PHP?

So I have a form where 2 out of 6 fields are visible to the user. The user can then click a button to reveal the other fields.
Each field uses the following PHP validation (Note: the preg_match is there to make sure they have entered a space as it's a full name field):
$multipleFormErrors = array();
if (!isset($firstGuestName) || empty($firstGuestName) || !preg_match("/ /",
$firstGuestName)) {
$multipleFormErrors["firstGuestName"] = "You have not entered your full name.";
}
if (!isset($secondGuestName) || empty($secondGuestName) || !preg_match("/ /",
$secondGuestName)) {
$multipleFormErrors["secondGuestName"] = "You have not entered guest #2's full
name.";
}
if (!isset($thirdGuestName) || empty($thirdGuestName) || !preg_match("/ /",
$thirdGuestName)) {
$multipleFormErrors["thirdGuestName"] = "You have not entered guest #3's full name.";
}
And so on up until guest #6.
The results are then being echoed to the user using:
if (isset($_POST["multipleSubmit"])) {
if ($multipleFormErrors) {
echo "<div class=\"errors\">";
echo "Please fix the following errors:";
echo "<ul>";
foreach ($multipleFormErrors as $error) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>";
echo "</div>";
}
}
The issue here is that all of the errors will display even if guests 3 - 6 aren't visible to the user. So If they submit the form with just the initial 2 guests filled out they will get an error because guests 3 - 6 have a value of an empty string. I think a way around this would be for PHP to detect whether the display value is set to block like you can do in JS so is this possible or do I need to do something different?
Cheers!
PHP happens on the server, Javascript happens on the client, and that's the crux of your issue. The server has no way (without a lot more coding and state tracking) to know if the client is looking at something or not.
I recommend:
Keeping your general application structure the way it is (don't do that state tracking, which would require a lot more JS/jQuery/etc)
Perhaps put your error code with the text box, so that the error only shows if the text box does
Code your system with the full realization that guests (beyond the first?) are optional, so guest checking should only occur on server side if there is a partial name (As it is, the error shows if the guest is blank, which will often happen). A blank name for guests 2-6 is probably completely legitimate.

Display an "offline" message when a external page is offline (PHP)

I have an number of ads displaying on my website that reads the balance of a Blockchain.info bitcoin address but their system for keeping the address balances online keeps going down.
What I want is to do is display a message when the address (not the website) can't be read.
The balance is always between 0 and 10 (and can be a decimal within that range).
I will save you reading all the curl code and just post what I have at the end of my PHP:
if ($BitcoinAddressBalance >= -0 && $BitcoinAddressBalance < 10) {
echo 'Online';
} else {
echo 'Offline';
}
My problem is that the code echoes 'Online' even when there is no data/numbers or I use a unregistered domain.
Can I use curl to check a page on a website has a number between 0 and 10?
If you want to check whether if the value in $BitcoinAddressBalance is not empty then you can use the following:
if ( isset($BitcoinAddressBalance) ) {
echo 'Online';
} else {
echo 'Offline';
}

PHP - Validating a registration form

How do I validate the first name to only contain a-z characters using php. I have the following at the moment:
if (empty($myFirstName)) {
echo "<p>Please enter your first name!</p>";
else if(!preg_match("/^[a-zA-Z]$/", $myFirstName)){
echo "Your first name can only contain letters!";
}
Here's a working code :
if (empty($myFirstName)) {
echo "<p>Please enter your first name!</p>";}
else if(preg_match("/[^a-zA-Z]/", $myFirstName)){
echo "Your first name can only contain letters!";
}
I did a little modification to the regex : I added a ^ in the group, and removed the anchors.
As a result, your regex will match any character which is not a letter, and display the error message if there is a match.
I strongly advice you to validate user input at least on server side, and on client side if you want to.
For an email validation, the html filter works on client side.
On server side, you can use pre-set PHP filters :
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "email OK";
}
FYI, there is a regexp matching emails according to the RFC2822 standards :
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
May be you can use
if(! ctype_alpha($myFirstName) )
{
//check for alphabetic inputs only
echo "Your first name can only contain letters!";
}
Php manual
Your if is incorrect.
!preg_match("/^[a-zA-Z]$/", $myFirstName)
will only hold true if $myFirstName is not a single alpha character
to ensure that $myFirstName is not any number of alpha characters, try
!preg_match("/^[a-zA-Z]*$/", $myFirstName)

Categories