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.

Slides:



Advertisements
Similar presentations
Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman.
Advertisements

Python: Regular Expressions
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 2: 8/23.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Scripting Languages Chapter 8 More About Regular Expressions.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
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.
Regular Expressions Dr. Ralph D. Westfall May, 2011.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009.
Finding the needle(s) in the textual haystack
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
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,
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expressions.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
VBScript Session 13.
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.
Module 6 – Generics Module 7 – Regular Expressions.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Satisfy Your Technical Curiosity Regular Expressions Roy Osherove Methodology & Team System Expert Sela Group The.
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.
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,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
CSC 4630 Meeting 21 April 4, Return to Perl Where are we? What is confusing? What practice do you need?
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.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Java Script Pattern Matching Using Regular Expressions.
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.
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. ADVANCED.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
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.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
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.
RE Tutorial.
Regular Expressions 'RegEx'.
CS 330 Class 7 Comments on Exam Programming plan for today:
Looking for Patterns - Finding them with Regular Expressions
Pattern Matching in Strings
Lecture 25: Regular Expressions
Validation using Regular Expressions
Presentation transcript:

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 task becomes even less enjoyable due to the lack of useful intrinsic validation functions in JavaScript. Fortunately, JavaScript 1.2+ has incorporated regular expressions. In this article I will present a brief tutorial on the basics of regular expressions and then give some examples of how they can be used to simplify data validation.

2 Regular expressions are very powerful tools for performing pattern matches. So how are regular expressions implemented in JavaScript? There are two ways: 1) Using literal syntax. 2) When you need to dynamically construct the regular expression, via the RegExp() constructor. The literal syntax looks something like: var RegularExpression = /pattern/ The RegExp() constructor method looks like var RegularExpression = new RegExp("pattern");

3 An Example Patterns are defined using string literal characters and metacharacters. For example, the following regular expression determines whether a string contains a valid 5-digit US postal code. function checkpostal(){ var re=/^\d{5}$/ if (document.myform.myinput.value.search(re)==-1) alert("Please enter a valid 5 digit number inside form") }

Pattern Matching Characters 4 Pattern-matching characters can be grouped into various categories, which will be explained in following. Symbol DescriptionExample ^Only matches the beginning of a string./^The/ matches "The" in "The night" by not "In The Night" $Only matches the end of a string./and$/ matches "and" in "Land" but not "landing" \bMatches any word boundary (test characters must exist at the beginning or end of a word within the string) /ly\b/ matches "ly" in "This is really cool." \BMatches any non-word boundary./\Bor/ matches “or” in "normal" but not "origami." \sMatch any single space character.

Pattern Matching Characters 5 Symbol DescriptionExample [xyz]Match any one character enclosed in the character set. You may use a hyphen to denote range. For example. /[a-z]/ matches any letter in the alphabet, /[0-9]/ any single digit. /[AN]BC/ matches "ABC" and "NBC" but not "BBC" since the leading “B” is not in the set. [^xyz]Match any one character not enclosed in the character set. The caret indicates that none of the charactersNOTE: the caret used within a character class is not to be confused with the caret that denotes the beginning of a string. Negation is only performed within the square brackets. /[^AN]BC/ matches "BBC" but not "ABC" or "NBC"..(Dot). Match any character except newline or another Unicode line terminator. /b.t/ matches "bat", "bit", "bet" and so on. \wMatch any alphanumeric character including the underscore. Equivalent to [a-zA-Z0-9_]. /\w/ matches "200" in "200%"

Pattern Matching Characters 6 Symbol DescriptionExample {x}Match exactly x occurrences of a regular expression. /\d{5}/ matches 5 digits. {x,}Match x or more occurrences of a regular expression. /\s{2,}/ matches at least 2 whitespace characters. {x,y}Matches x to y number of occurrences of a regular expression. /\d{2,4}/ matches at least 2 but no more than 4 digits. ?Match zero or one occurrences. Equivalent to {0,1}. /a\s?b/ matches "ab" or "a b". \dMatch any single digit. Equivalent to [0-9]. \DMatch any non-digit. Equivalent to [^0-9]./\D/ matches "No" in "No "

Pattern Matching Characters 7 Symbol DescriptionExample *Match zero or more occurrences. Equivalent to {0,}. /we*/ matches "w" in "why" and "wee" in "between", but nothing in "bad" +Match one or more occurrences. Equivalent to {1,}. /fe+d/ matches both "fed" and "feed" ( )Grouping characters together to create a clause. May be nested. /(abc)+(def)/ matches one or more occurrences of "abc" followed by one occurrence of "def". |Alternation combines clauses into one regular expression and then matches any of the individual clauses. Similar to "OR" statement. /(ab)|(cd)|(ef)/ matches "ab" or "cd" or "ef". iIgnore the case of characters./The/i matches "the" and "The" and "tHe" gGlobal search for all occurrences of a pattern/ain/g matches both "ain"s in "No pain no gain", instead of just the first.

8 Examples: 1-Valid Number A valid number value should contain only an optional minus sign, followed by digits, followed by an optional dot (.) to signal decimals, and if it's present, additional digits. A regular expression to do that would look like this: var anum=/(^-*\d+$)|(^-*\d+\.\d+$)/ 2- Valid Date Format A valid short date should consist of a 2-digit month, date separator, 2- digit day, date separator, and a 4-digit year (e.g. 02/02/2000). It would be nice to allow the user to use any valid date separator character that your backend database supported such as slashes, dashes and periods. function checkdateformat(userinput){ var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/ return dateformat.test(userinput) }

9 Examples with String Functions: 1. var string1="Peter has 8 dollars and Jane has 15" parsestring1=string1.match(/\d+/g) //returns the array [8,15] var string2="(304) " parsestring2=string2.replace(/[\(\)-]/g, "") //Returns " " (removes "(", ")", and "-") var string3="1,2, 3, 4, 5" parsestring3=string3.split(/\s*,\s*/) //Returns the array ["1","2","3","4","5"]

10 StringPattern for Mattching z]{2,}$/i IP Address[0-9]\{1,3\}\. ???? Only Alphabet/[^a-zA-Z]/g Only 0-9/[\D]/g Alphabet and 0-9 /[^a-zA-Z0-9]/g Phone Number/^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.- ]?\d{4}$/ Date Formatd{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/ US zip coded{5}$)|(^\d{5}-\d{4}$)/ Examples with Pattern Matching: