FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
Introducing JavaScript
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Tutorial 6 Creating a Web Form
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 14 Working with Forms and Regular Expressions.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Creating Web Page Forms
Introduction to scripting
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
15. User Authentication, Form Validation, Paging. M. Udin Harun Al Rasyid, S.Kom, Ph.D
. If the PHP server is an server or is aware of which server is the server, then one can write code that s information. –For example,
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Last Updated March 2006 Slide 1 Regular Expressions.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
JavaScript Form Validation
PHP : Hypertext Preprocessor
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Tutorial 14 Working with Forms and Regular Expressions.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Project Four Forms Discuss form processing Describe the difference between client-side and server-side form processing Add a horizontal rule to a Web page.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
1 HTML Forms
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Form Processing Week Four. Form Processing Concepts The principal tool used to process Web forms stored on UNIX servers is a CGI (Common Gateway Interface)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Creating Web Page Forms COE 201- Computer Proficiency.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 6 JavaScript: Introduction to Scripting
Looking for Patterns - Finding them with Regular Expressions
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Introduction to Scripting
Web Programming– UFCFB Lecture 17
Working with Forms and Regular Expressions
Conditions and Ifs BIS1523 – Lecture 8.
WEB PROGRAMMING JavaScript.
PHP.
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Validation using Regular Expressions
CIS 136 Building Mobile Apps
Presentation transcript:

FORM VALIDATION Faheem Ahmed Khokhar

Topics To be Covered Introducing Server side Validation Checking Empty fields Checking field lengths Checking Ranges Checking formats (with Regular Expressions)

Validation? The act of validating; finding or testing the truth of something. Or The act of declaring or making legally valid Validation is the process of checking if something satisfies a certain standard/ criteria.

FORM VALIDATION Form validation is the process of checking that a form has been filled in correctly before it is processed. For example, if your form has a box for the user to type their email address, you might want your form handler to check that they've filled in their address before you deal with the rest of the form There are two main methods for validating forms: server-side (using Common Gateway Interface (CGI) scripts, ASP, etc), and client-side (usually done using JavaScript). Server-side validation is more secure but often more tricky to code, whereas client-side (JavaScript) validation is easier to do and quicker too (the browser doesn't have to connect to the server to validate the form, so the user finds out instantly if they've missed out that required field!).

Introduction to Server-side validation Server-side data validation means using PHP to verify that valid information has been sent to the script. Using server-side validation has pretty much the exact opposite pros and cons of client-side development: it is more secure and works seamlessly with all browsers, but it does so at the cost of slightly higher server load and slower feedback for users.

CHECKING EMPTY FIELDS Users are irritating. They don't like filling out forms, and will tear through them as fast as they possibly can to get to the fun part of your site. Since they are typing so fast, they probably won't read the directions and sometimes they leave the fields blank and submit the forms. To avoid inserting blank fields in the data base, we bind them to fill all the required fields

CHECKING EMPTY FIELDS Example: ?> <?php $var=""; if(empty($var)) { echo "The variable is empty"; } else echo "The variable is having some value"; ?>

Enter CNIC number without using (-) signs Assignment Enter Last Name Enter Name Enter CNIC number without using (-) signs Please fill the field

Output

Regular expression types There are 2 types of regular expressions: POSIX (Portable Operating System Interface for uniX) Extended Perl Compatible The ereg, eregi, ... are the POSIX versions. The preg_match, preg_replace, ... are the Perl version. It is important that using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/). However this version is more powerful and faster as well than the POSIX one.

Precautions We will be using PCRE. When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. Often used delimiters are forward slashes (/), hash/number signs (#) and tildes (~). The pattern should be written inside double quotation(“ “)

Regular expressions syntax [abc] a, b, or c [a-z] Any lowercase letter [^A-Z] Any character that is not a uppercase letter [a-z]+ One or more lowercase letters [0-9.-] Any number, dot, or minus sign ^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _ [^A-Za-z0-9] Any symbol (not a number or a letter) ([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers

Pattern Switches use switches to make the match global or case- insensitive or both: Switches are added to the very end of a regular expression. Property Description Example i Ignore the case of character /The/i matches "the" and "The" and "tHe"

PHP Preg_match() Function This function matches the value given by the user and defined in the regular expression. If the regular expression and the value given by the user, becomes equal, the function will return true, false otherwise. Syntax: Preg_match(Pattern, Subject, regs) Pattern – Pattern is used to search the string. Subject – input given by the user. regs If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs, the matches will be stored in the elements of the array regs.

Literal Characters match themselves. The Carrot/Circumflex Sign ^ Means string must start with. preg_match(“/^hidaya/”,”hidaya trust”) The Dollar $ sign Means string must end with. preg_match(“/hidaya$/”,”hidaya trust”) The Period . sign Means match any charcter. preg_match(“/^de.r/”,”dear”)

preg_match() continued… EXAMPLE <?php $pattern= "/trust$/"; $string = "hidaya trust"; echo preg_match($pattern,$string); ?>

preg_match() continued Example 2 <?php $date="2012-2-3"; $regs="-"; if (preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $date, $regs)) { echo "$regs[3].$regs[2].$regs[1]"; } else { echo "Invalid date format: $date"; ?>

Pattern matching Example 1: <?php $pattern="/^[A-Za-z ]{1,}$/"; $subject="Hidaya Trust"; if(preg_match($pattern,$subject)) { echo "Pattern Matched"; } else echo "Pattern Mismatched"; ?>

Description In Example 1, the following are valid and acceptable by the pattern/validation process. ^ carrot sign shows that the string must start with small or capital alpha numeric characters and space can be added in the pattern matching. / slashes are for start and end of pattern. $ sign will check the pattern

Example 2: <?php $pattern="/^[A-Z ]{1,}$/i"; $subject="hidayatrust"; if(preg_match($pattern,$subject)) { echo "Pattern Matched"; } else echo "Pattern Mismatched"; ?>

Description In Example 2 The case of characters will be ignored by the pattern It will match only the required pattern

Example 3: <?php $pattern="/^[A-Z ]{1,}\.$/i"; $subject="hidaya trust."; if(preg_match($pattern,$subject)) { echo "Pattern Matched"; } else echo "Pattern Mismatched"; ?>

Description In Example 3 The case of characters will be ignored by the pattern It will match only the required pattern The dot(.) is compulsory in the end of the string

PHP ereg( ) Function Searches a string for matches to the regular expression given in pattern in a case-sensitive way. Syntax ereg ( string $pattern , string $string [, array &$regs ] ) pattern : Case sensitive regular expression. string The input string. regs If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs, the matches will be stored in the elements of the array regs.

ereg() continued.. <?php $date="2012-3-22"; $regs="-"; if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { echo "$regs[3].$regs[2].$regs[1]"; } else { echo "Invalid date format: $date"; ?>

preg_replace() This function performs the search and replaces the string. It works like str_replace() Syntax preg_replace(Pattern, Replacement, String/Array) Pattern : It is used to search for. It can be either a string or an array with string. Replacement : The string or an array with string to replace. If this parameter is a string and the pattern parameter is an array, all pattern will be replace by that string. If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra pattern will be replaced by an empty string. String/Array – input given by the user

PHP preg_replace() Function EXAMPLE <?php $pattern= "/trust$/"; $replacement = "foundation"; $string = "hidaya trust"; echo preg_replace($pattern,$replacement,$string); ?>

preg_replace() $pattern="/^[a-z ]+$/"; $string="faheem ahmed"; $a=preg_match($pattern,$string); if($a) { $replacement="Ali"; $patt="/ahmed/"; echo preg_replace($patt,$replacement,$string); }

CHECKING FIELD LENGTH To restrict the users to fill the forms within the boundary of the requirements To implement server-side validation, we write a PHP script that handles the validation and then process the data accordingly. The user will be bound to enter data within the limit. You are very familiar to string functions, they are utilized in the validation section

CHECKING FIELD LENGTH Example: $text="Fah123"; $pattern="/^[0-9a-zA-Z]{6}$/"; echo preg_match($pattern,$text); Or $text="123456"; $pattern="/^[0-9]{6}$/";

CHECKING FIELD RANGES Checking the field ranges is one of the important part of the validation. The user has to insert the data in between the range of the defined length.

CHECKING FIELD RANGES Example: <?php $text="123456789012"; $pattern="/^[0-9]{6,12}$/"; echo preg_match($pattern,$text); ?>

Assignment 2

Message should be displayed, if pattern does not match criteria Out put Message should be displayed, if pattern does not match criteria

Out Put

Validate Form with Built-in Fuctions

PHP filter_var() Function The filter_var() function filters a variable with the specified filter. Returns the filtered data on success or FALSE on failure. Syntax filter_var(variable, filter, options) Parameter Description variable Required. Specifies the variable to filter filter Optional. Specifies the ID of the filter to use. Default is FILTER_SANITIZE_STRING.  options Optional. Specifies an associative array of flags/options or a single flag/option. Check each filter for possible options and flags

The filters to be used in validation are Number (Integer) Validation Number (Integer) validation with range String Validation with Regular Expression Email Validation

Number (Integer) Validation <?php $integer="6234"; if(filter_var($integer,FILTER_VALIDATE_INT)) { echo "Integer Number"; } else echo "Not Integer"; ?>

Number (Integer) Validation with range <?php $integer="6234"; if(filter_var($integer,FILTER_VALIDATE_INT, array("options"=>array("min_range"=>1,"max_range"=>5000)))) { echo "Integer Number"; } else echo "Not Integer"; ?>

String Validation with Regular Expression <?php $regExp="/^[a-zA-Z ]{1,}$/"; if((filter_var($string,FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>$regExp))))) { echo "Expression Matched"; } else echo "Expression Not Matched"; ?>

Email Validation <?php $email="faheem_khokhar2004@hotmail.com"; if(filter_var($email,FILTER_VALIDATE_EMAIL)) { echo "Valid Email"; } else echo "invalid Email"; ?>