INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Ch3: Introduction to HTML5 part 2 Dr. Abdullah Almutairi ISC 340 Fall 2014.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Tutorial 14 Working with Forms and Regular Expressions.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Introduction to scripting
CST JavaScript Validating Form Data with JavaScript.
slides created by Marty Stepp
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
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.
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.
Tutorial 14 Working with Forms and Regular Expressions.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 10 – Working with Forms Modern JavaScript Design And Develop Copyright © 2012 by Larry.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
Programming Languages Meeting 13 December 2/3, 2014.
Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.
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.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
JavaScript, Part 2 Instructor: Charles Moen CSCI/CINF 4230.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and.
CS346 Regular Expressions1 Pattern Matching Regular Expression.
IS2802 Introduction to Multimedia Applications for Business Lecture 5: JavaScript, form validation and browser detection Rob Gleasure
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
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.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
LING 408/508: Programming for Linguists Lecture 14 October 19 th.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
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.
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.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
HTML 5 Form elements Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
Data Structures Arrays and Lists Part 2 More List Operations.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
Assignment #2. Regular Expression (RE) Represent a string pattern – Consists of regular characters and wild cards Assignment #2: implement a subset of.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
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.
Java Script Date Object
JavaScript.
>> Introduction to JavaScript
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 6 JavaScript: Introduction to Scripting
In this session, you will learn about:
SEEM4570 Tutorial 05: JavaScript as OOP
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
WEB PROGRAMMING JavaScript.
Functions, Regular expressions and Events
Presentation transcript:

INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1

Outline JavaScript RegExp object JavaScript RegExp object HTML5 Form field Validation With RegExp HTML5 Form field Validation With RegExp 2

JavaScript RegExp Object Regular expressions are patterns used to match character combinations and perform search-and-replace functions in strings. In JavaScript, regular expressions are also objects. RegExp – is short for regular expression. – is the JavaScript built-in object. 3

Creating RegExp Object Syntax – Pattern: the text of the regular expression. – Modifiers: if specified, modifiers can have any combination of the following values: g - global match i - ignore case m - multiline; 4 var patt=new RegExp(pattern[, modifiers]); or : var patt=/pattern/modifiers;

String Method – match() A String method that executes a search for a match in a string. It returns an array of the found text value. or null on a mismatch. Example 1 5 var str = "Welcome to Toronto"; var patt1 = new RegExp("to", "i"); // i: ignore case-sensitivity var result = str.match(patt1); alert(result); // to

String Method – match() Example 2 Example 3 6 var str = "Welcome to Toronto"; var patt1 = /to/g; // g: do a global search var result = str.match(patt1); alert(result); // to,to var str = "Welcome to Toronto"; var patt1 = /to/ig; // ig: global and case-insensitive var myArray = str.match(patt1); var msg = ""; for(var i=0; i<myArray.length; i++) { msg += "Found " + myArray[i] + "\n"; } alert(msg); // Found to // Found To // Found to

RegExp Method – test() A RegExp method that tests for a match in a string. It returns true or false. Example 7 var str = "Welcome to Toronto"; var patt1 = new RegExp("Me"); var patt2 = new RegExp("Me","i"); var result1 = patt1.test(str); var result2 = patt2.test(str); alert(result1 + "\n" + result2); // false // ture

RegExp Method – exec() A RegExp method that executes a search for a match in a string. – It returns an array of the found text value. – If no match is found, it returns null. Example 8 var myRe = /ab*/g; var str = "abbcdefabh"; var myArray; var msg = ""; while ((myArray = myRe.exec(str)) !== null) { msg += "Next match starts at " + myRe.lastIndex + " -- "; msg += "Found " + myArray[0] + ".\n"; } a lert(msg); // Next match starts at 3 -- Found abb. // Next match starts at 9 -- Found ab.

More RegExp Methods searchA String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails. replaceA String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. splitA String method that uses a regular expression or a fixed string to break a string into an array of substrings. 9

Special characters in regular expressions charactermeaning ^String begin $String end.Match – one character ?Match – zero or one (preceding character) *Match – zero or more +Match – one or more {m, n}Match – m or n number of preceding character [ ]Character sets delimiters [ ] \d= [0-9], Match – numbers \w= [A-Za-z0-9_], Match – alphanumeric \s= [ \t\r\n], Match – whitespace 10

RegExp Examples To validate: minimum of 4 alphabetical numbers – var pattern1 = /^[a-zA-Z]{4,}$/; To validate: telephone format ###-###-#### – var pattern2 = /^([0-9]{3}[-]){2}[0-9]{4}$/; Example 11 var patt1 = /^[a-zA-Z]{4,}$/; while(1){ var str = prompt("Please enter your last name",""); if(str) { if(patt1.test(str)) alert("Your Last Name: " + str); else alert(“Characters only and at least 4! Try again."); } else break; }

HTML5 Form field Validation With RegExp regular expression HTML 5 introduces pattern attribute to all the various form fields to which regular expression validation applies. Other attributes for form validation – required attribute - handles whether leaving the field empty is valid or not – title attribute can be used as a tooltip – placeholder attribute 12

HTML5 Form field Validation Example – Runs in Chrome browser – Code (on next page) 13

14 HTML Forms - RegExp Validation HTML Forms - RegExp Validation <form id=form1 method=POST action= User Name: <input type=text name=username required pattern="^[a-zA-Z]{4,}$" title="Characters only and minimum 4 characters."> Phone Number: <input type=text name=phonenumber placeholder="###-###-####" required pattern="^([0-9]{3}[-]){2}[0-9]{4}$" title="Please follow the format showed.">

Thank You! 15