Validation using Regular Expressions Regular Expression Instead of
- Slides: 14
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-z. A-Z]+/", $first. Name))
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 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-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
Allowed: starts with letters
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. A-Z]*)*$
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. Script if(! first. Name. match(pattern)) • Determines whether the string variable first. Name matches the pattern determined by the regular expression
- Quadratic formula examples
- Xkcd regex
- How do you simplify a rational expression
- Perl6 失敗
- Regular language
- Regular expressions wikipedia
- Algebraic properties of regular expression
- I formal
- Primitive regular expressions
- Construction of epsilon nfa from regular expression
- Sed regular expression examples
- Regular expression () meaning
- Regular expression recursive definition
- Regular expression of even even language
- Regular expression for palindrome