Validation using Regular Expressions

Slides:



Advertisements
Similar presentations
Session 3BBK P1 ModuleApril 2010 : [#] Regular Expressions.
Advertisements

FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
. 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,
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
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.
PHP : Hypertext Preprocessor
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
1 DIG 3134 – Lecture 10: Regular Expressions and preg_match in PHP and Validating Inputs Michael Moshell University of Central Florida Internet Software.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Perl Day 4. Fuzzy Matches We know about eq and ne, but they only match things exactly We know about eq and ne, but they only match things exactly –Sometimes.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Radio Buttons. Input/Form/Radio Group Use the dialog to enter label and values for the radio buttons.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Regular Expressions In Javascript cosc What Do They Do? Does pattern matching on text We use the term “string” to indicate the text that the regular.
JavaScript: Conditionals contd.
C++ Memory Management – Homework Exercises
JavaScript.
Perl Regular Expression in SAS
Strings and Serialization
Looking for Patterns - Finding them with Regular Expressions
Lecture 19 Strings and 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 C++ October 2, 2017.
Web Programming– UFCFB Lecture 17
Agenda Control Flow Statements Purpose test statement
Working with Forms and Regular Expressions
Boolean Bingo!.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
Logical Operations In Matlab.
Variables Kevin Harville.
Data Manipulation & Regex
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
PHP PART 2.
Regular Expressions grep Familiy of Commands
Common Lisp II.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

Validation using Regular Expressions

Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows a particular pattern. For example, is it a phone number? The patterns are known as regular expressions. They can be confusing, but there are libraries of them – so you don’t have to come up with your own if your data follows a well-known pattern

PHP preg_match if(!preg_match("/[a-zA-Z]+/",$firstName))

if(!preg_match("/[a-zA-Z]+/",$firstName)) The simplest version of preg_match takes two arguments The first is a regular expression pattern that is placed between “/ and /” The second is the string variable in which one searches for the pattern The function returns true if the pattern is found, false if the pattern is not found The exclamation point ! Is the not operator – used here because we want to do something if the pattern is not found

The pattern: [a-zA-Z]+ The square brackets indicate a set of characters. The hyphen indicates a range. Thus this pattern is a-z or A-Z, in other words a small or capital letter. The + sign indicates that there should be one or more letters in the pattern But there can be other things in the pattern

Blocked

Let through

if(!preg_match("/^[a-zA-Z]+/",$firstName)) Adding the caret ^ indicates that the string variable should not just have a string of one or more letters but it should start with a string of one or more letters.

Blocked: Doesn’t start with letters

Allowed: starts with letters

if(!preg_match("/^[a-zA-Z]+$/",$firstName)) Adding the dollar sign $ indicates that the string variable should not just have a string of one or more letters but it should end with a string of one or more letters. So now it should begin and end with letters – nothing else is allowed. That may be too strong, it doesn’t allow for spaces, hyphens or apostrophes

Regular Expression library: http://regexlib.com ^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$

Testing a regular expression on the client side function validateForm() { var firstName=document.getElementById("txtFirstName").value; var lastName=document.getElementById("txtLastName").value; var pattern = new RegExp(/^[a-zA-Z]+$/); if(! firstName.match(pattern)) alert("Please enter a proper first name."); return false; } else if(! lastName.match(pattern)) alert("Please enter a proper last name.");

var pattern = new RegExp(/^[a-zA-Z]+$/); Declares a regular expression in JavaScript if(! firstName.match(pattern)) Determines whether the string variable firstName matches the pattern determined by the regular expression