SlideShare a Scribd company logo
FORM VALIDATION
client side validation
INTRODUCING CLIENT SIDE VALIDATION
– Checking Empty Fields ...
– Generating Alerts For Invalid Data …
– Practice And Assignments …
muhammadabaloch
INTRODUCING CLIENT SIDE VALIDATION
– When you create forms, providing form validation is useful to ensure
that your customers enter valid and complete data.
– For example, you may want to ensure that someone inserts a valid e-
mail address into a text box, or perhaps you want to ensure that
someone fills in certain fields.
muhammadabaloch
INTRODUCING CLIENT SIDE VALIDATION
– Client-side validation provides validation within the browser on client
computers through JavaScript. Because the code is stored within the page or
within a linked file,
– it is downloaded into the browser when a user accesses the page and,
therefore, doesn't require a round-trip to the server. For this reason,
– client form validation can be faster than server-side validation. However,
– client-side validation may not work in all instances. A user may have a browser
that doesn't support client-side scripting or may have scripting disabled in the
browser. Knowing the limits of client-side scripting helps you decide whether
to use client-side form validation.
muhammadabaloch
ENABLE JAVASCRIPT
• Google Chrome
– Click the wrench icon on the browser toolbar.
– Select Options
– then click Settings.
– Click the Show Advance Settings tab.
– Click Content settings in the "Privacy" section.
– JavaScript
• Allow all sites to run JavaScript (recommended)
• Do not allow any site to run JavaScript (incase to disable JavaScript)
muhammadabaloch
ENABLE JAVASCRIPT
• Mozilla Firefox 3.6+
– Click the Tools menu.
– Select Options.
– Click the Content tab.
– Select the 'Enable JavaScript' checkbox.
– Click the OK button.
muhammadabaloch
CHECKING EMPTY FIELDS
<head>
<script language=“javascript” type=“text/javascript”>
function chkEmpty()
{
var name= document.myform.txtName;
var LName=document.myform.txtLName;
If(name.value==“”)
{
document.getElementById(“txtNameMsg”).innerHTML=“Please fill the field”;
name.focus();
}
else If(LName.value==“”)
{
document.getElementById(“txtLNameMsg”).innerHTML=“Please fill the field”;
LName.focus();
}
muhammadabaloch
CHECKING EMPTY FIELDS
else
{
document.myform.submit();
}
}
</script>
</head>
<html>
<body>
<form action=“process.php” method=“post” name=“myform”>
<table>
<tr><td><input type=“text” name=“txtName”></td>
<td id=“txtNameMsg”></td></tr>
<tr><td><input type=“text” name=“txtLName”></td>
<td id=“txtLNameMsg”></td></tr>
<tr><td><input type=“button” name=“btn” value=“Submit”></td></tr>
</table>
</form>
</body>
</html>
muhammadabaloch
REGULAR EXPRESSIONS
– Regular expressions are very powerful tools for performing pattern
matches. PERL programmers and UNIX shell programmers have
enjoyed the benefits of regular expressions for years. Once you
master the pattern language, most validation tasks become trivial. You
can perform complex tasks that once required lengthy procedures
with just a few lines of code using regular expressions.
– So how are regular expressions implemented in JavaScript?
– There are two ways:
muhammadabaloch
REGULAR EXPRESSIONS
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/
– while the RegExp() constructor method looks like
– var RegularExpression = new RegExp("pattern");
– The RegExp() method allows you to dynamically construct the search
pattern as a string, and is useful when the pattern is not known ahead
of time.
muhammadabaloch
REGULAR EXPRESSIONS (syntax)
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not
a uppercase letter
[a-z]+ One or more lowercase
letters
[0-9.-] Any number, dot, or minus
sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one
letter, number or _
[^A-Za-z0-9] Any symbol (not a number
or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four
numbers
muhammadabaloch
REGULAR EXPRESSIONS (Metacharacters)
Metacharacter Description
w Finds word character i.e. (alphabets
and number)
W Finds non-word character i.e. (Special
Characters)
d Finds digit i.e. (Numbers)
D Finds non-digit character i.e.
(alphabets and Special Characters)
muhammadabaloch
REGULAR EXPRESSIONS (Pattern Switches or Modifiers)
– use switches to make the match global or case- insensitive or both:
– Switches are added to the very end of a regular expression.
Property Description Example
i Ignore the case of
character
/The/i matches "the"
and "The" and "tHe"
g Global search for all
occurrences of a
pattern
/ain/g matches both
"ain"s in "No pain no
gain", instead of just
the first.
 gi Global search, ignore
case.
/it/gi matches all "it"s
in "It is our IT
department" 
muhammadabaloch
REGULAR EXPRESSIONS (JavaScript RegExp Object )
• What is RegExp … ?
– A regular expression is an object that describes a pattern of characters.
– When you search in a text, you can use a pattern to describe what you are
searching for.
– A simple pattern can be one single character.
– A more complicated pattern can consist of more characters, and can be used for
parsing, format checking, substitution and more.
– Regular expressions are used to perform powerful pattern-matching and "search-
and-replace" functions on text.
Syntax
var patt= new RegExp( pattern , modifiers );
or more simply:
var patt=/pattern/modifiers;
muhammadabaloch
HOW TO CREATE PATTERNS
var pattern = new RegExp(/^[a-z ]{1,}$/);
or
var pattern = /^[a-z]{1,}$/;
var pattern = new RegExp(pattern);
or
var pattern = new RegExp(“^[a-z]{1,}$”);
or
var pattern = “^[a-z]{1,}$”;
var pattern = new RegExp($pattern);
– It will accept only a to z alphabets or only alpha numeric characters, having
minimum length of 1.
muhammadabaloch
HOW TO CREATE PATTERNS (Continued…..)
var pattern = new RegExp(/^[0-9]{1,}$/);
or
var pattern = /^[0-9]{1,}$/;
var pattern = new RegExp(pattern);
or
var pattern = new RegExp(“^[0-9]{1,}$”);
or
var pattern = “^[0-9]{1,}$”;
var pattern = new RegExp(pattern);
– It will accept only numbers between 0 to 9 or only numeric
characters, having minimum length of 1.
muhammadabaloch
RegExp OBJECT METHOD ( test() )
– The test() method tests for a match in a string.
– This method returns true if it finds a match, otherwise it returns false.
Syntax
RegExpObject.test(string)
Parameter Description
string Required. The string to be searched
muhammadabaloch
RegExp OBJECT METHOD ( test() )
<script type="text/javascript">
var str="Hello world!";
var patt=/Hello/g;
var result=patt.test(str);
document.write("Returned value: " + result);
</script>
<script type="text/javascript">
var pattern =new RegExp(/^[a-z]{1,}$/);
var string = "hello world";
var result = pattern.test(string);
document.write("The result = "+ result);
</script>
muhammadabaloch
RegExp OBJECT METHOD (exec() )
– Executes method
– The exec() method tests for a match in a string.
– This method returns the matched text if it finds a match, otherwise it
returns null.
Syntax
RegExpObject.exec(string)
Parameter Description
string Required. The string to be searched
muhammadabaloch
RegExp OBJECT METHOD (exec() )
<script type="text/javascript">
var str="Hello world!";
var patt=/Hello/g;
var result=patt.exec(str);
document.write("Returned value: " + result);
</script>
<script language="javascript" type="text/javascript">
var str="The rain in SPAIN stays mainly in the plain";
var pattern = new RegExp(/[a-z]{1,}/gi);
var result = pattern.exec(str);
document.write("The result = "+ result);
</script>
muhammadabaloch
STRING OBJECT METHOD (replace())
– The replace() method searches a string for a specified value, or
a regular expression, and returns a new string where the specified
values are replaced.
Syntax
string.replace( searchvalue , newvalue )
Parameter Description
searchvalue Required. The value, or regular
expression, that will be replaced by
the new value
newvalue Required. The value to replace the
searchvalue with
muhammadabaloch
STRING OBJECT METHOD (replace())
<script language="javascript" type="text/javascript">
var str="Mr Blue has a blue house and a blue car";
var n=str.replace(/blue/g,"red");
document.write(n);
</script>
muhammadabaloch
STRING OBJECT METHOD (match())
– The match() method searches a string for a match against a regular
expression, and
returns the matches, as an Array object.
– Note: If the regular expression does not include the g modifier (to perform
a global search),
– the match() method will return only the first match in the string.
– This method returns null if no match is found.
Syntax
string.match(regexp)
– The return type of match() method is array.
Parameter Description
regexp Required. The value to search for, as a
regular expression.
muhammadabaloch
STRING OBJECT METHOD (match())
<script language="javascript" type="text/javascript">
var str="The rain in SPAIN stays mainly in the plain";
var result=str.match(/ain/gi);
document.write("The result = "+ result);
</script>
• The result of n will be:
• ain,AIN,ain,ain
muhammadabaloch
STRING OBJECT METHOD (match())
<script language="javascript" type="text/javascript">
var pattern = new RegExp(/ain/gi);
var result = str.match(pattern);
document.write("The result = "+ result);
</script>
The result of n will be:
ain,AIN,ain,ain
muhammadabaloch
STRING OBJECT METHOD (match())
<script language="javascript" type="text/javascript">
var str ="The rain in SPAIN stays mainly in the plain";
var pattern = new RegExp(/ [a-z]{1,}/gi);
var result = str.match(pattern);
document.write("The result = "+ result);
</script>
– The result of n will be:
– The,rain,in,SPAIN,stays,mainly,in,the,plain
muhammadabaloch
STRING OBJECT METHOD (search())
– The search() method searches a string for a specified value, or regular
expression, and returns the position of the match.
– This method returns -1 if no match is found.
Syntax
string.search(searchvalue)
Parameter Description
searchvalue Required. The value, or regular
expression, to search for.
muhammadabaloch
STRING OBJECT METHOD (search())
– The return type is integer.
var str=“Hidaya Trust!";
var n=str.search(“Trust");
– The result of n will be:
– 7
muhammadabaloch
STRING OBJECT METHOD (search())
<script language="javascript" type="text/javascript">
var str="Mr Blue has a blue house and a blue car";
var result=str.search(/blue/g);
document.write(result);
</script>
muhammadabaloch
ASSIGNMENT
Submit
First Name
Last Name
CNIC #
Enter Last Name
Enter Name
Enter CNIC
number
Invalid Format, Only alpha
numeric characters are alowed
Please fill the
field
muhammadabaloch
OUTPUT
muhammadabaloch

More Related Content

What's hot (20)

PPTX
Html forms
Er. Nawaraj Bhandari
 
PDF
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
PPT
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
PDF
Html forms
eShikshak
 
PPTX
HTML Forms
Ravinder Kamboj
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PPTX
Dynamic HTML (DHTML)
Himanshu Kumar
 
PPTX
Inner classes in java
PhD Research Scholar
 
PDF
Php Tutorials for Beginners
Vineet Kumar Saini
 
PPTX
Web design - Working with forms in HTML
Mustafa Kamel Mohammadi
 
PPT
Introduction to JavaScript
Andres Baravalle
 
PPTX
Html form tag
shreyachougule
 
PDF
Quick flask an intro to flask
juzten
 
PPT
Span and Div tags in HTML
Biswadip Goswami
 
PPTX
DTD
Kamal Acharya
 
PPTX
Html forms
Himanshu Pathak
 
PDF
2. HTML forms
Pavle Đorđević
 
PPTX
Javascript 101
Shlomi Komemi
 
PPTX
Bootstrap 3
Lanh Le
 
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Html forms
eShikshak
 
HTML Forms
Ravinder Kamboj
 
Javascript variables and datatypes
Varun C M
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Dynamic HTML (DHTML)
Himanshu Kumar
 
Inner classes in java
PhD Research Scholar
 
Php Tutorials for Beginners
Vineet Kumar Saini
 
Web design - Working with forms in HTML
Mustafa Kamel Mohammadi
 
Introduction to JavaScript
Andres Baravalle
 
Html form tag
shreyachougule
 
Quick flask an intro to flask
juzten
 
Span and Div tags in HTML
Biswadip Goswami
 
Html forms
Himanshu Pathak
 
2. HTML forms
Pavle Đorđević
 
Javascript 101
Shlomi Komemi
 
Bootstrap 3
Lanh Le
 

Viewers also liked (10)

PPTX
Form Validation
Graeme Smith
 
PPTX
Javascript validating form
Jesus Obenita Jr.
 
PPTX
Form Validation in JavaScript
Ravi Bhadauria
 
DOCX
Javascript validation karan chanana
karanchanan
 
PDF
Javascript validation assignment
H K
 
PPT
Form validation server side
Mudasir Syed
 
PPT
Qualification & validation concept & terminology
Muhammad Luqman Ikram
 
PPT
Qualification & Validation
ICHAPPS
 
PPTX
Computer System Validation
Eric Silva
 
PPTX
Concept of URS,DQ,IQ,OQ,PQ
dhavalrock24
 
Form Validation
Graeme Smith
 
Javascript validating form
Jesus Obenita Jr.
 
Form Validation in JavaScript
Ravi Bhadauria
 
Javascript validation karan chanana
karanchanan
 
Javascript validation assignment
H K
 
Form validation server side
Mudasir Syed
 
Qualification & validation concept & terminology
Muhammad Luqman Ikram
 
Qualification & Validation
ICHAPPS
 
Computer System Validation
Eric Silva
 
Concept of URS,DQ,IQ,OQ,PQ
dhavalrock24
 
Ad

Similar to Form validation client side (20)

PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PPT
Regular expressions
Raj Gupta
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PDF
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
PPT
regular-expressions lecture 28-string regular expression
smallboss311
 
PPTX
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
 
PPTX
Regular expression unit2
smitha273566
 
DOCX
WD programs descriptions.docx
anjani pavan kumar
 
PDF
Validating forms (and more) with the HTML5 pattern attribute
cliener
 
PDF
JavaScript Essentials in 1 Hour (2018)
Ahmed Ibrahim
 
PDF
Regular expression in javascript
Toan Nguyen
 
PPTX
Regular Expressions
Akhil Kaushik
 
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
PPTX
JSregularExpressions.pptx
MattMarino13
 
PPTX
Regular expressions using Python
Md. Shafiuzzaman Hira
 
PPTX
IWP GRUP 18.pptx
DEBROOPSARKAR20MEI10
 
PPT
9781305078444 ppt ch08
Terry Yoast
 
PPTX
JavaScript.pptx
Govardhan Bhavani
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Regular expressions
Raj Gupta
 
3.2 javascript regex
Jalpesh Vasa
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
regular-expressions lecture 28-string regular expression
smallboss311
 
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
 
Regular expression unit2
smitha273566
 
WD programs descriptions.docx
anjani pavan kumar
 
Validating forms (and more) with the HTML5 pattern attribute
cliener
 
JavaScript Essentials in 1 Hour (2018)
Ahmed Ibrahim
 
Regular expression in javascript
Toan Nguyen
 
Regular Expressions
Akhil Kaushik
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
JSregularExpressions.pptx
MattMarino13
 
Regular expressions using Python
Md. Shafiuzzaman Hira
 
IWP GRUP 18.pptx
DEBROOPSARKAR20MEI10
 
9781305078444 ppt ch08
Terry Yoast
 
JavaScript.pptx
Govardhan Bhavani
 
Ad

More from Mudasir Syed (20)

PPT
Error reporting in php
Mudasir Syed
 
PPT
Cookies in php lecture 2
Mudasir Syed
 
PPT
Cookies in php lecture 1
Mudasir Syed
 
PPTX
Ajax
Mudasir Syed
 
PPT
Reporting using FPDF
Mudasir Syed
 
PPT
Oop in php lecture 2
Mudasir Syed
 
PPT
Oop in php lecture 2
Mudasir Syed
 
PPT
Filing system in PHP
Mudasir Syed
 
PPT
Time manipulation lecture 2
Mudasir Syed
 
PPT
Time manipulation lecture 1
Mudasir Syed
 
PPT
Php Mysql
Mudasir Syed
 
PPT
Adminstrating Through PHPMyAdmin
Mudasir Syed
 
PPT
Sql select
Mudasir Syed
 
PPT
PHP mysql Sql
Mudasir Syed
 
PPT
PHP mysql Mysql joins
Mudasir Syed
 
PPTX
PHP mysql Introduction database
Mudasir Syed
 
PPT
PHP mysql Installing my sql 5.1
Mudasir Syed
 
PPT
PHP mysql Er diagram
Mudasir Syed
 
PPT
PHP mysql Database normalizatin
Mudasir Syed
 
PPT
PHP mysql Aggregate functions
Mudasir Syed
 
Error reporting in php
Mudasir Syed
 
Cookies in php lecture 2
Mudasir Syed
 
Cookies in php lecture 1
Mudasir Syed
 
Reporting using FPDF
Mudasir Syed
 
Oop in php lecture 2
Mudasir Syed
 
Oop in php lecture 2
Mudasir Syed
 
Filing system in PHP
Mudasir Syed
 
Time manipulation lecture 2
Mudasir Syed
 
Time manipulation lecture 1
Mudasir Syed
 
Php Mysql
Mudasir Syed
 
Adminstrating Through PHPMyAdmin
Mudasir Syed
 
Sql select
Mudasir Syed
 
PHP mysql Sql
Mudasir Syed
 
PHP mysql Mysql joins
Mudasir Syed
 
PHP mysql Introduction database
Mudasir Syed
 
PHP mysql Installing my sql 5.1
Mudasir Syed
 
PHP mysql Er diagram
Mudasir Syed
 
PHP mysql Database normalizatin
Mudasir Syed
 
PHP mysql Aggregate functions
Mudasir Syed
 

Recently uploaded (20)

PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 

Form validation client side

  • 2. INTRODUCING CLIENT SIDE VALIDATION – Checking Empty Fields ... – Generating Alerts For Invalid Data … – Practice And Assignments … muhammadabaloch
  • 3. INTRODUCING CLIENT SIDE VALIDATION – When you create forms, providing form validation is useful to ensure that your customers enter valid and complete data. – For example, you may want to ensure that someone inserts a valid e- mail address into a text box, or perhaps you want to ensure that someone fills in certain fields. muhammadabaloch
  • 4. INTRODUCING CLIENT SIDE VALIDATION – Client-side validation provides validation within the browser on client computers through JavaScript. Because the code is stored within the page or within a linked file, – it is downloaded into the browser when a user accesses the page and, therefore, doesn't require a round-trip to the server. For this reason, – client form validation can be faster than server-side validation. However, – client-side validation may not work in all instances. A user may have a browser that doesn't support client-side scripting or may have scripting disabled in the browser. Knowing the limits of client-side scripting helps you decide whether to use client-side form validation. muhammadabaloch
  • 5. ENABLE JAVASCRIPT • Google Chrome – Click the wrench icon on the browser toolbar. – Select Options – then click Settings. – Click the Show Advance Settings tab. – Click Content settings in the "Privacy" section. – JavaScript • Allow all sites to run JavaScript (recommended) • Do not allow any site to run JavaScript (incase to disable JavaScript) muhammadabaloch
  • 6. ENABLE JAVASCRIPT • Mozilla Firefox 3.6+ – Click the Tools menu. – Select Options. – Click the Content tab. – Select the 'Enable JavaScript' checkbox. – Click the OK button. muhammadabaloch
  • 7. CHECKING EMPTY FIELDS <head> <script language=“javascript” type=“text/javascript”> function chkEmpty() { var name= document.myform.txtName; var LName=document.myform.txtLName; If(name.value==“”) { document.getElementById(“txtNameMsg”).innerHTML=“Please fill the field”; name.focus(); } else If(LName.value==“”) { document.getElementById(“txtLNameMsg”).innerHTML=“Please fill the field”; LName.focus(); } muhammadabaloch
  • 8. CHECKING EMPTY FIELDS else { document.myform.submit(); } } </script> </head> <html> <body> <form action=“process.php” method=“post” name=“myform”> <table> <tr><td><input type=“text” name=“txtName”></td> <td id=“txtNameMsg”></td></tr> <tr><td><input type=“text” name=“txtLName”></td> <td id=“txtLNameMsg”></td></tr> <tr><td><input type=“button” name=“btn” value=“Submit”></td></tr> </table> </form> </body> </html> muhammadabaloch
  • 9. REGULAR EXPRESSIONS – Regular expressions are very powerful tools for performing pattern matches. PERL programmers and UNIX shell programmers have enjoyed the benefits of regular expressions for years. Once you master the pattern language, most validation tasks become trivial. You can perform complex tasks that once required lengthy procedures with just a few lines of code using regular expressions. – So how are regular expressions implemented in JavaScript? – There are two ways: muhammadabaloch
  • 10. REGULAR EXPRESSIONS 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/ – while the RegExp() constructor method looks like – var RegularExpression = new RegExp("pattern"); – The RegExp() method allows you to dynamically construct the search pattern as a string, and is useful when the pattern is not known ahead of time. muhammadabaloch
  • 11. REGULAR EXPRESSIONS (syntax) [abc] a, b, or c [a-z] Any lowercase letter [^A-Z] Any character that is not a uppercase letter [a-z]+ One or more lowercase letters [0-9.-] Any number, dot, or minus sign ^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _ [^A-Za-z0-9] Any symbol (not a number or a letter) ([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers muhammadabaloch
  • 12. REGULAR EXPRESSIONS (Metacharacters) Metacharacter Description w Finds word character i.e. (alphabets and number) W Finds non-word character i.e. (Special Characters) d Finds digit i.e. (Numbers) D Finds non-digit character i.e. (alphabets and Special Characters) muhammadabaloch
  • 13. REGULAR EXPRESSIONS (Pattern Switches or Modifiers) – use switches to make the match global or case- insensitive or both: – Switches are added to the very end of a regular expression. Property Description Example i Ignore the case of character /The/i matches "the" and "The" and "tHe" g Global search for all occurrences of a pattern /ain/g matches both "ain"s in "No pain no gain", instead of just the first.  gi Global search, ignore case. /it/gi matches all "it"s in "It is our IT department"  muhammadabaloch
  • 14. REGULAR EXPRESSIONS (JavaScript RegExp Object ) • What is RegExp … ? – A regular expression is an object that describes a pattern of characters. – When you search in a text, you can use a pattern to describe what you are searching for. – A simple pattern can be one single character. – A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more. – Regular expressions are used to perform powerful pattern-matching and "search- and-replace" functions on text. Syntax var patt= new RegExp( pattern , modifiers ); or more simply: var patt=/pattern/modifiers; muhammadabaloch
  • 15. HOW TO CREATE PATTERNS var pattern = new RegExp(/^[a-z ]{1,}$/); or var pattern = /^[a-z]{1,}$/; var pattern = new RegExp(pattern); or var pattern = new RegExp(“^[a-z]{1,}$”); or var pattern = “^[a-z]{1,}$”; var pattern = new RegExp($pattern); – It will accept only a to z alphabets or only alpha numeric characters, having minimum length of 1. muhammadabaloch
  • 16. HOW TO CREATE PATTERNS (Continued…..) var pattern = new RegExp(/^[0-9]{1,}$/); or var pattern = /^[0-9]{1,}$/; var pattern = new RegExp(pattern); or var pattern = new RegExp(“^[0-9]{1,}$”); or var pattern = “^[0-9]{1,}$”; var pattern = new RegExp(pattern); – It will accept only numbers between 0 to 9 or only numeric characters, having minimum length of 1. muhammadabaloch
  • 17. RegExp OBJECT METHOD ( test() ) – The test() method tests for a match in a string. – This method returns true if it finds a match, otherwise it returns false. Syntax RegExpObject.test(string) Parameter Description string Required. The string to be searched muhammadabaloch
  • 18. RegExp OBJECT METHOD ( test() ) <script type="text/javascript"> var str="Hello world!"; var patt=/Hello/g; var result=patt.test(str); document.write("Returned value: " + result); </script> <script type="text/javascript"> var pattern =new RegExp(/^[a-z]{1,}$/); var string = "hello world"; var result = pattern.test(string); document.write("The result = "+ result); </script> muhammadabaloch
  • 19. RegExp OBJECT METHOD (exec() ) – Executes method – The exec() method tests for a match in a string. – This method returns the matched text if it finds a match, otherwise it returns null. Syntax RegExpObject.exec(string) Parameter Description string Required. The string to be searched muhammadabaloch
  • 20. RegExp OBJECT METHOD (exec() ) <script type="text/javascript"> var str="Hello world!"; var patt=/Hello/g; var result=patt.exec(str); document.write("Returned value: " + result); </script> <script language="javascript" type="text/javascript"> var str="The rain in SPAIN stays mainly in the plain"; var pattern = new RegExp(/[a-z]{1,}/gi); var result = pattern.exec(str); document.write("The result = "+ result); </script> muhammadabaloch
  • 21. STRING OBJECT METHOD (replace()) – The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Syntax string.replace( searchvalue , newvalue ) Parameter Description searchvalue Required. The value, or regular expression, that will be replaced by the new value newvalue Required. The value to replace the searchvalue with muhammadabaloch
  • 22. STRING OBJECT METHOD (replace()) <script language="javascript" type="text/javascript"> var str="Mr Blue has a blue house and a blue car"; var n=str.replace(/blue/g,"red"); document.write(n); </script> muhammadabaloch
  • 23. STRING OBJECT METHOD (match()) – The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object. – Note: If the regular expression does not include the g modifier (to perform a global search), – the match() method will return only the first match in the string. – This method returns null if no match is found. Syntax string.match(regexp) – The return type of match() method is array. Parameter Description regexp Required. The value to search for, as a regular expression. muhammadabaloch
  • 24. STRING OBJECT METHOD (match()) <script language="javascript" type="text/javascript"> var str="The rain in SPAIN stays mainly in the plain"; var result=str.match(/ain/gi); document.write("The result = "+ result); </script> • The result of n will be: • ain,AIN,ain,ain muhammadabaloch
  • 25. STRING OBJECT METHOD (match()) <script language="javascript" type="text/javascript"> var pattern = new RegExp(/ain/gi); var result = str.match(pattern); document.write("The result = "+ result); </script> The result of n will be: ain,AIN,ain,ain muhammadabaloch
  • 26. STRING OBJECT METHOD (match()) <script language="javascript" type="text/javascript"> var str ="The rain in SPAIN stays mainly in the plain"; var pattern = new RegExp(/ [a-z]{1,}/gi); var result = str.match(pattern); document.write("The result = "+ result); </script> – The result of n will be: – The,rain,in,SPAIN,stays,mainly,in,the,plain muhammadabaloch
  • 27. STRING OBJECT METHOD (search()) – The search() method searches a string for a specified value, or regular expression, and returns the position of the match. – This method returns -1 if no match is found. Syntax string.search(searchvalue) Parameter Description searchvalue Required. The value, or regular expression, to search for. muhammadabaloch
  • 28. STRING OBJECT METHOD (search()) – The return type is integer. var str=“Hidaya Trust!"; var n=str.search(“Trust"); – The result of n will be: – 7 muhammadabaloch
  • 29. STRING OBJECT METHOD (search()) <script language="javascript" type="text/javascript"> var str="Mr Blue has a blue house and a blue car"; var result=str.search(/blue/g); document.write(result); </script> muhammadabaloch
  • 30. ASSIGNMENT Submit First Name Last Name CNIC # Enter Last Name Enter Name Enter CNIC number Invalid Format, Only alpha numeric characters are alowed Please fill the field muhammadabaloch