Reader
  • Home
  • About
  • Deals
  • Services
  • Contact
You are here: Home / Archives for validate

PHP: How to validate a telephone number

Last updated on July 9th, 2009 by Gabriel Livan

This snippet is useful if you need to validate a telephone number using an editable list with specific formats.

<?php
/*
string validate_telephone_number (string $number, array $formats)
*/

function validate_telephone_number($number, $formats)
{
$format = trim(ereg_replace("[0-9]", "#", $number));

return (in_array($format, $formats)) ? true : false;
}

/* Usage Examples */

// List of possible formats: You can add new formats or modify the existing ones

$formats = array('###-###-####', '####-###-###',
				 '(###) ###-###', '####-####-####',
				 '##-###-####-####', '####-####', '###-###-###',
				 '#####-###-###', '##########');

$number = '08008-555-555';

if(validate_telephone_number($number, $formats))
{
echo $number.' is a valid phone number.';
}

echo "<br />";

$number = '123-555-555';

if(validate_telephone_number($number, $formats))
{
echo $number.' is a valid phone number.';
}

echo "<br />";

$number = '1800-1234-5678';

if(validate_telephone_number($number, $formats))
{
echo $number.' is a valid phone number.';
}

echo "<br />";

$number = '(800) 555-123';

if(validate_telephone_number($number, $formats))
{
echo $number.' is a valid phone number.';
}

echo "<br />";

$number = '1234567890';

if(validate_telephone_number($number, $formats))
{
echo $number.' is a valid phone number.';
}
?>

Filed Under: PHP Tagged With: validate

PHP: Check (validate) if the Uploaded File is an Image

Last updated on March 15th, 2015 by Gabriel Livan

In this tutorial I will show you how to create an image validator script. You can choose between 2 methods of validation: one that will verify if the file is actually an image, by checking the file’s mime-type, and the other one which checks the extension of the uploaded file.

Let’s start creating the configuration file:

config.php

<?php
/* 
1 = Check if the file uploaded is actually an image no matter what extension it has
2 = The uploaded files must have a specific image extension
*/

$validation_type = 1;

if($validation_type == 1)
{
	$mime = array('image/gif' => 'gif',
                  'image/jpeg' => 'jpeg',
                  'image/png' => 'png',
                  'application/x-shockwave-flash' => 'swf',
                  'image/psd' => 'psd',
                  'image/bmp' => 'bmp',
                  'image/tiff' => 'tiff',
                  'image/tiff' => 'tiff',
                  'image/jp2' => 'jp2',
                  'image/iff' => 'iff',
                  'image/vnd.wap.wbmp' => 'bmp',
                  'image/xbm' => 'xbm',
                  'image/vnd.microsoft.icon' => 'ico');
}
else if($validation_type == 2) // Second choice? Set the extensions
{
	$image_extensions_allowed = array('jpg', 'jpeg', 'png', 'gif','bmp');
}

$upload_image_to_folder = 'images/';
?>

Now let’s create the form that will help us to upload the file:

<form enctype="multipart/form-data" action="validate_image_upload.php" method="POST">
    
<!-- MAX_FILE_SIZE must be set before the input element -->
<input type="hidden" name="MAX_FILE_SIZE" value="2048000" />

<!-- The name from the $_FILES array is determined by the input name -->
Select an Image: <input name="image_file" type="file" /> 
<input type="submit" value="Upload" />

</form>

Our form will send the data to validate_image_upload.php. Here, the script will check the type of validation. If the file is validated the user will see a successful submission message and the file will be moved in the specified image folder. The uploaded file failed to pass the validation process? In this case, it will be deleted and the script will output an error message.

Read more from this entry…

Filed Under: PHP Tagged With: validate

Validating an image upload

Last updated on July 15th, 2015 by Gabriel Livan

This is a useful JavaScript function which validates an image upload by checking the extension of the file that should be uploaded. If the extension is jpg, jpeg, png or gif it will return true. Otherwise, it will return false.

UPDATE This is a basic function to remind the users to upload an image with the right extension. You shouldn’t rely only on this method to validate an image upload as checking the MIME type in the backend (if, for instance, you’re using a scripting language such as PHP) is highly recommended. Backend validation is often a must and it applies to everything, not only file uploads.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Validate image on upload @ BitRepository.com</TITLE>
  <META NAME="Author" CONTENT="Bit Repository">

  <META NAME="Keywords" CONTENT="validate, extensions, file, javascript">
  <META NAME="Description" CONTENT="A JavaScript Extension Validator for Images">
 
<SCRIPT LANGUAGE="JavaScript">

<!--
function validate()
{
var extensions = new Array("jpg","jpeg","gif","png","bmp");

/*
// Alternative way to create the array

var extensions = new Array();

extensions[1] = "jpg";
extensions[0] = "jpeg";
extensions[2] = "gif";
extensions[3] = "png";
extensions[4] = "bmp";
*/

var image_file = document.form.image_file.value;

var image_length = document.form.image_file.value.length;

var pos = image_file.lastIndexOf('.') + 1;

var ext = image_file.substring(pos, image_length);

var final_ext = ext.toLowerCase();

for (i = 0; i < extensions.length; i++)
{
    if(extensions[i] == final_ext)
    {
    return true;
    }
}

alert("You must upload an image file with one of the following extensions: "+ extensions.join(', ') +".");
return false;
}
 //-->

 </SCRIPT>

 </HEAD>

<BODY>

<center>

<form name="form" action="http://www.microsoft.com" enctype="multipart/form-data" method="post" onSubmit="return validate();">

<h2>Validate image on upload</h2>

<br />

	Upload an image: <INPUT type="file" name="image_file"> <input type="submit" name="submit" value="Submit">

</form>

</center>

</BODY>

</HTML>

NOTE: You can add more extensions to the validator by continuing the extensions array.

Good luck!


Filed Under: JavaScript Tagged With: validate

AJAX Form with CAPTCHA, Realtime Validation and PHP Backend

Last updated on February 7th, 2016 by Gabriel Livan

The aim of this basic tutorial is to help you to create a simple (table-less) AJAX Contact Form using jQuery & PHP. It will give you an idea of how you can create similar web applications using jQuery and AJAX. We will have a HTML page which will contain the form, a CSS file, a .PHP page where the data will be sent and another file where the validation function(s) will be located.

  • Download
  • View Demo
    • Basic
    • Advanced
    • Premium

Step 1 – Creating the configuration file

config.php

<?php
// To
define("WEBMASTER_EMAIL", "your_name@your-domain.com");
?>

Here you should fill the e-mail address where you wish to receive the mail as well as the address where you wish to receive the replies (usually the same).

Step 2 – Creating the css file

style.css

/*
Credits: Bit Repository
CSS Library: http://www.bitrepository.com/
*/

html, body  { padding: 0; border: 0px none; }

.notification_error {
	border: 1px solid #A25965;
	height: auto;
	width: 90%;
	padding: 4px;
	background: #F8F0F1;
	text-align: left;
	-moz-border-radius: 5px;
}

.notification_ok {
	border: 1px #567397 solid;
	height: auto;
	width: 90%
	padding: 8px;
	background: #f5f9fd;
	text-align: center;
	-moz-border-radius: 5px;
}

.info_fieldset { -moz-border-radius: 7px; border: 1px #dddddd solid; }

.info_fieldset legend {
	border: 1px #dddddd solid;
	color: black; 

	font: 13px Verdana;

	padding: 2px 5px 2px 5px;
	-moz-border-radius: 3px;
}

.button {
	border: 1px solid #999999;
	border-top-color: #CCCCCC;
	border-left-color: #CCCCCC; 

	background: white;

	color: #333333; 

	font: 11px Verdana, Helvetica, Arial, sans-serif;

	-moz-border-radius: 3px;
}

/* Label */
label {width: 140px; padding-left: 20px; margin: 5px; float: left; text-align: left;}

/* Input, Textarea */
input, textarea {
	margin: 5px;
	padding: 0px;
	float: left;

	border: 1px solid #999999;
	border-top-color: #CCCCCC;
	border-left-color: #CCCCCC; 

	color: #333333; 

	font: 11px Verdana, Helvetica, Arial, sans-serif;

	-moz-border-radius: 3px;
}

/* BR */

br { clear: left; }

Read more from this entry…

Filed Under: AJAX, jQuery, PHP Tagged With: ajax form builder, form, tableless, validate

Validate (input) password

Last updated on July 7th, 2009 by Gabriel Livan

This is a useful function if you need to validate a password (input). If you have a form and you need to check if the username, which is registering, entered a valid password (without illegal characters) then this would help you to do it. Only letters, numbers and underscores are accepted.

<?php
/*
Credits: http://www.bitrepository.com/
*/

// default chars values are defined
function validate_password($password, $min_char = 4, $max_char = 20)
{
// Remove whitespaces from the beginning and end of a string
$password = trim($password); 

// Accept only letters, numbers and underscore
$eregi = eregi_replace('([a-zA-Z0-9_]{'.$min_char.','.$max_char.'})', 
'', $password);

if(empty($eregi))
	{
	return true;
	}
	else
	{
	return false;
	}
}

$password = 'my_password_123';

/*
First parameter: Password
Second parameter: Minimum chars 
Third parameter: Maximum chars
*/

$validator = validate_password($password, 4, 20);

if($validator) 
{
// Our current example will return true
echo 'The password is valid.';
}
else 
{
/* if our password value is for instance: my_@#%_password 
our function will return false */
echo 'The password appears to be invalid. 
It should contain only letters, numbers and underscore.';
}
?>

Filed Under: PHP Tagged With: validate

Next Page »

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact