Validation using Regular Expressions Regular Expression Instead of

  • Slides: 14
Download presentation
Validation using Regular Expressions

Validation using Regular Expressions

Regular Expression • Instead of asking if user input has some particular value, sometimes

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-z. A-Z]+/", $first. Name))

PHP preg_match if(!preg_match("/[a-z. A-Z]+/", $first. Name))

if(!preg_match("/[a-z. A-Z]+/", $first. Name)) • The simplest version of preg_match takes two arguments –

if(!preg_match("/[a-z. A-Z]+/", $first. Name)) • 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-z. A-Z]+ • The square brackets indicate a set of characters. •

The pattern: [a-z. A-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

Blocked

Let through

Let through

if(!preg_match("/^[a-z. A-Z]+/", $first. Name)) • Adding the caret ^ indicates that the string variable

if(!preg_match("/^[a-z. A-Z]+/", $first. Name)) • 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

Blocked: Doesn’t start with letters

Allowed: starts with letters

Allowed: starts with letters

if(!preg_match("/^[a-z. A-Z]+$/", $first. Name)) • Adding the dollar sign $ indicates that the string

if(!preg_match("/^[a-z. A-Z]+$/", $first. Name)) • 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-z. A-Z]+(([', . - ][a-z. A-Z ])? [a-z.

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

Testing a regular expression on the client side function validate. Form() { var first.

Testing a regular expression on the client side function validate. Form() { var first. Name=document. get. Element. By. Id("txt. First. Name"). value; var last. Name=document. get. Element. By. Id("txt. Last. Name"). value; var pattern = new Reg. Exp(/^[a-z. A-Z]+$/); if(! first. Name. match(pattern)) { alert("Please enter a proper first name. "); return false; } else if(! last. Name. match(pattern)) { alert("Please enter a proper last name. "); return false; } }

var pattern = new Reg. Exp(/^[a-z. A-Z]+$/); • Declares a regular expression in Java.

var pattern = new Reg. Exp(/^[a-z. A-Z]+$/); • Declares a regular expression in Java. Script if(! first. Name. match(pattern)) • Determines whether the string variable first. Name matches the pattern determined by the regular expression