Validation using Regular Expressions. Regular Expression Instead of asking if user input has some...
-
Upload
angel-barker -
Category
Documents
-
view
218 -
download
0
Transcript of Validation using Regular Expressions. Regular Expression Instead of asking if user input has some...
![Page 1: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/1.jpg)
Validation using Regular Expressions
![Page 2: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/2.jpg)
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
![Page 3: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/3.jpg)
PHP preg_match
if(!preg_match("/[a-zA-Z]+/",$firstName))
![Page 4: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/4.jpg)
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
![Page 5: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/5.jpg)
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
![Page 6: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/6.jpg)
Blocked
![Page 7: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/7.jpg)
Let through
![Page 8: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/8.jpg)
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.
![Page 9: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/9.jpg)
Blocked: Doesn’t start with letters
![Page 10: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/10.jpg)
Allowed: starts with letters
![Page 11: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/11.jpg)
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
![Page 12: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/12.jpg)
Regular Expression library: http://regexlib.com
^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$
![Page 13: Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/13.jpg)
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;}
}
![Page 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.](https://reader035.fdocuments.in/reader035/viewer/2022070414/5697c0251a28abf838cd4f06/html5/thumbnails/14.jpg)
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