SlideShare a Scribd company logo
Validation using Regular Expression (JS)
Let’s see some basic notes:
1. Regular Expression must be written between / /;
 Example: /road2code/;
 It will find match for road2code in string.
2. [abc] it will find match for any character present in [ ].
 Example: /[abc123]/;
 It will accept single a, single b or single 1,2,3
 It will only specify one-character matching
3. Allow all character from a-z (upper case + lower case)
 Example: /[a-zA-Z]/;
 It will accept any alphabet from a-z (upper case+lower
case)
4. All character Except Some character
 Example: /[^ab]/;
 It will accept c-z not a and b
 Example: /[^rs]000/;
 It will not accept r000 or s000 it will accept a000 or b000
and so on.
5. Allow word with 5 character only
 Example: /[a-zA-Z]{5}/;
 It will accept Hello
 It will not accept aHello
6. Allow only 5digits (Pin code validation)
 Example: /[0-9]{5}/;
 It will accept: 360005
 It will not accept: 25684572
7. Must Start with Alphabet ‘a’:
 Example:/^a/;
 It will accept: abcasadf
 It will not accept:bcadae
8. Must End with Alphabet ‘a’:
 Example:/a$/;
 It will accept: abcderfga
 It will not accept: abcdefg
9. Must be a “Hello” word:
 Example: /^Hello$/;
 It will accept: Hello
 It will not accept: hello
Need to know
w: [a-zA-z0-9_] (character+digits+_)
s: White space, tabs
t: tab
d: digits [0-9]
+ : 1 or more occurrence
? : 0 or more occurrence
[] : Character Set
[^]: exclude character set
 : escape character
1)Mobile Number Validation: (10 digits only)
Expression: /^[0-9]{10}$/;
Examples:
1.1234567895 Accepted
2.12asdfa257 Rejected
3.123456789798 Rejected
Instead of writing [0-9] every time we can write d also.
Expression for Mobile validation: /^d{10}$/;
2)Pin Code Validation: (5digits only)
Expression: /^[0-9]{5}$/;
Examples:
1.360005 Accepted
2.12asdfa257 Rejected
3.abcdefg Rejected
Email id Validation:
Email id consist of:
(name) @ (domain_name) . (com/co/net) . (in/uk/au)
Example: upadhyayraj007@gmail.com
Here
Name = upadhyayraj007
@
Domain_name = gmail
.
Extension = com
So now let’s build this one by one
1)Name
It may have alphabets digits or ‘.’ Or ‘-‘
So for this Expression:
/^[a-zA-Zd.-]+$/;
So it means One or more occurrence of a-z or 0-9 or . or -
2)@
Expression: /^[a-zA-Zd.-]+@$/;
3)(domain name)
Expression: ([a-zd.-]+)
Combine with first two expressions:
/^[a-zA-Zd.-]+@([a-zd.-]+)$/;
4) .
Expression: .
Up to this:
/^[a-zA-Zd.-]+@([a-zd.-]+).$/;
5) (com,net,co…etc)
Expression: ([a-z]{2,8})
There must be at least 2 character and maximum 8 charater.
Expression : /^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})$/;
6) optional (.uk,.in,.au…etc)
Expression: (.[a-z]{2,8})?
There must be . then some character but min length 2 and
max length 8 and we put ? in last because it’s option many of
us don’t have such email id like admin@road2code.co.in
Final Expression:
Expression: /^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})(.[a-z]{2,8})?$/;
HTML CODE for Email validation
<html>
<body>
<form name="registration" method="POST" onSubmit="return form1()">
<input type="text" name="email">
<input type="submit" value="login">
</form>
<script>
function form1(){
var email=document.registration.email.value;
var email_chk=emailchk(email);
if(email_chk==false){
alert("Invalid Email id");
}
else{
alert("valid Email id");
}
}
function emailchk(em){
var pattern1=/^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})(.[a-z]{2,8})?$/;
var match1=new RegExp(pattern1);
return match1.test(em);
}
</script>
</body>
</html>

More Related Content

PDF
slide share test
PPTX
regex_presentation.pptx
PDF
Regular expression presentation for the HUB
PPTX
Regular expressions
PDF
Practical JavaScript Programming - Session 6/8
PDF
Validating forms (and more) with the HTML5 pattern attribute
PPT
Regular expressions
PDF
And Now You Have Two Problems
slide share test
regex_presentation.pptx
Regular expression presentation for the HUB
Regular expressions
Practical JavaScript Programming - Session 6/8
Validating forms (and more) with the HTML5 pattern attribute
Regular expressions
And Now You Have Two Problems

Similar to JavaScript Regular Expression Match (20)

PDF
And now you have two problems. Ruby regular expressions for fun and profit by...
PDF
How to check valid Email? Find using regex.
PPTX
02. input validation module v5
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
PDF
Regex - Regular Expression Basics
PDF
How to check valid email? Find using regex(p?)
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PDF
How to check valid Email? Find using regex.
PPTX
Regular Expression
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
PDF
3.2 javascript regex
KEY
PPTX
Javascript regular expression
PDF
Headless Js Testing
PDF
Regular Expressions: QA Challenge Accepted Conf (March 2015)
PPT
Regular Expressions 2007
PPT
Form validation client side
PPTX
Regular expressions tutorial for SEO & Website Analysis
PDF
Java Regular Expression PART II
And now you have two problems. Ruby regular expressions for fun and profit by...
How to check valid Email? Find using regex.
02. input validation module v5
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
Regex - Regular Expression Basics
How to check valid email? Find using regex(p?)
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
How to check valid Email? Find using regex.
Regular Expression
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
3.2 javascript regex
Javascript regular expression
Headless Js Testing
Regular Expressions: QA Challenge Accepted Conf (March 2015)
Regular Expressions 2007
Form validation client side
Regular expressions tutorial for SEO & Website Analysis
Java Regular Expression PART II
Ad

More from raj upadhyay (12)

PPTX
Basics of java (1)
PPTX
Zed attack proxy [ What is ZAP(Zed Attack Proxy)? ]
PPTX
Folder Can't Delete How to Remove FILES That Won't Delete?
PPTX
Recovering unallocated space of a usb flash drive
PPTX
Terminal commands ubuntu 2
PPTX
Terminal Commands (Linux - ubuntu) (part-1)
PPTX
Find out Which Versions of the .NET Framework are Installed on a PC.
PPTX
Tree Traversals (In-order, Pre-order and Post-order)
PPTX
Relational Algebra,Types of join
PPTX
PL-SQL DIFFERENT PROGRAMS
PPT
How to get notification from google group
PPTX
Disadvantages of file management system (file processing systems)
Basics of java (1)
Zed attack proxy [ What is ZAP(Zed Attack Proxy)? ]
Folder Can't Delete How to Remove FILES That Won't Delete?
Recovering unallocated space of a usb flash drive
Terminal commands ubuntu 2
Terminal Commands (Linux - ubuntu) (part-1)
Find out Which Versions of the .NET Framework are Installed on a PC.
Tree Traversals (In-order, Pre-order and Post-order)
Relational Algebra,Types of join
PL-SQL DIFFERENT PROGRAMS
How to get notification from google group
Disadvantages of file management system (file processing systems)
Ad

Recently uploaded (20)

PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
introduction to high performance computing
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Soil Improvement Techniques Note - Rabbi
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
737-MAX_SRG.pdf student reference guides
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
distributed database system" (DDBS) is often used to refer to both the distri...
introduction to high performance computing
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
Abrasive, erosive and cavitation wear.pdf
Fundamentals of safety and accident prevention -final (1).pptx
Current and future trends in Computer Vision.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Visual Aids for Exploratory Data Analysis.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Soil Improvement Techniques Note - Rabbi
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
August 2025 - Top 10 Read Articles in Network Security & Its Applications
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
737-MAX_SRG.pdf student reference guides
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf

JavaScript Regular Expression Match

  • 1. Validation using Regular Expression (JS)
  • 2. Let’s see some basic notes: 1. Regular Expression must be written between / /;  Example: /road2code/;  It will find match for road2code in string. 2. [abc] it will find match for any character present in [ ].  Example: /[abc123]/;  It will accept single a, single b or single 1,2,3  It will only specify one-character matching 3. Allow all character from a-z (upper case + lower case)  Example: /[a-zA-Z]/;  It will accept any alphabet from a-z (upper case+lower case) 4. All character Except Some character  Example: /[^ab]/;  It will accept c-z not a and b  Example: /[^rs]000/;  It will not accept r000 or s000 it will accept a000 or b000 and so on. 5. Allow word with 5 character only  Example: /[a-zA-Z]{5}/;  It will accept Hello  It will not accept aHello 6. Allow only 5digits (Pin code validation)  Example: /[0-9]{5}/;
  • 3.  It will accept: 360005  It will not accept: 25684572 7. Must Start with Alphabet ‘a’:  Example:/^a/;  It will accept: abcasadf  It will not accept:bcadae 8. Must End with Alphabet ‘a’:  Example:/a$/;  It will accept: abcderfga  It will not accept: abcdefg 9. Must be a “Hello” word:  Example: /^Hello$/;  It will accept: Hello  It will not accept: hello
  • 4. Need to know w: [a-zA-z0-9_] (character+digits+_) s: White space, tabs t: tab d: digits [0-9] + : 1 or more occurrence ? : 0 or more occurrence [] : Character Set [^]: exclude character set : escape character
  • 5. 1)Mobile Number Validation: (10 digits only) Expression: /^[0-9]{10}$/; Examples: 1.1234567895 Accepted 2.12asdfa257 Rejected 3.123456789798 Rejected Instead of writing [0-9] every time we can write d also. Expression for Mobile validation: /^d{10}$/; 2)Pin Code Validation: (5digits only) Expression: /^[0-9]{5}$/; Examples: 1.360005 Accepted 2.12asdfa257 Rejected 3.abcdefg Rejected
  • 6. Email id Validation: Email id consist of: (name) @ (domain_name) . (com/co/net) . (in/uk/au) Example: upadhyayraj007@gmail.com Here Name = upadhyayraj007 @ Domain_name = gmail . Extension = com So now let’s build this one by one 1)Name It may have alphabets digits or ‘.’ Or ‘-‘ So for this Expression: /^[a-zA-Zd.-]+$/; So it means One or more occurrence of a-z or 0-9 or . or - 2)@ Expression: /^[a-zA-Zd.-]+@$/; 3)(domain name) Expression: ([a-zd.-]+) Combine with first two expressions: /^[a-zA-Zd.-]+@([a-zd.-]+)$/;
  • 7. 4) . Expression: . Up to this: /^[a-zA-Zd.-]+@([a-zd.-]+).$/; 5) (com,net,co…etc) Expression: ([a-z]{2,8}) There must be at least 2 character and maximum 8 charater. Expression : /^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})$/; 6) optional (.uk,.in,.au…etc) Expression: (.[a-z]{2,8})? There must be . then some character but min length 2 and max length 8 and we put ? in last because it’s option many of us don’t have such email id like admin@road2code.co.in Final Expression: Expression: /^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})(.[a-z]{2,8})?$/;
  • 8. HTML CODE for Email validation <html> <body> <form name="registration" method="POST" onSubmit="return form1()"> <input type="text" name="email"> <input type="submit" value="login"> </form> <script> function form1(){ var email=document.registration.email.value; var email_chk=emailchk(email); if(email_chk==false){ alert("Invalid Email id"); } else{ alert("valid Email id"); } } function emailchk(em){ var pattern1=/^([a-zd.-]+)@([a-zd.-]+).([a-z]{2,8})(.[a-z]{2,8})?$/; var match1=new RegExp(pattern1); return match1.test(em); } </script> </body> </html>