Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.

Slides:



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

FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
Chapter 14 Perl-Compatible Regular Expressions Part 1.
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 in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
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.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
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.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
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,
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
CS346 Regular Expressions1 Pattern Matching Regular Expression.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Powerpoint Templates Page 1 Powerpoint Templates GROUP 8:REGULAR EXPRESSION GURU BESAR: PN. SARINA SULAIMAN CIKGU-CIKGU: 1.CIKGU NENI 2.CIKGU
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 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
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.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
OVERVIEW OF CLIENT-SIDE SCRIPTING
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.
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.
Working with Forms and Regular Expressions
Boolean Bingo!.
Functions, Regular expressions and Events
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.
Validation using Regular Expressions
Common Lisp II.
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))

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: ^[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."); return false; }

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