Practical No 12
Practical No 12
Objective : To develop a web page for validation of form fields using regular expressions.
Resource Used
- Computer with a text editor (e.g., Notepad, Visual Studio Code)
Theory
Syntax
/pattern/modifiers;
Example
Quantifiers
Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more
occurrences of n n? Matches any string that contains zero or
one occurrences of n n{X} Matches any string that contains a
sequence of X n's
n{X,Y} Matches any string that contains a sequence
of X to Y n's n{X,} Matches any string that contains a
sequence of at least X n's n$ Matches any string with n at the
end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n
?!n Matches any string that is not followed by a specific string n
RegExp
Object
Properties
Property
Description
constructor Returns the function that created the RegExp object's prototype
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
Using test()
The following example searches a string for the character "e":
Example
var patt = /e/;
patt.test("The best things in life are free!");
Since there is an "e" in the string, the output of the code above will be:
true
You don't have to put the regular expression in a variable first. The two lines above can be
shortened to one:
/e/.test("The best things in life are free!");
Using exec()
The exec() method is a RegExp expression method.
It searches a string for a specified pattern, and returns the found text as an object. If no match is
found, it returns an empty (null) object.
The following example searches a string for the
character "e": Example 1
/e/.exec("The best things in life are free!");
Program :
1. Develop a web page for validation of form fields using regular expressions.
<!DOCTYPE html>
<html>
<head>
<title>creating mailing system</title>
<style>
legend {
display: block;
padding-left: 2px;
padding-right: 2px;
border: none;
}
</style>
<script type="text/javascript">
function validate() {
<body bgcolor="cyan">
<center>
<h1>Email Registration</h1>
<form>
<fieldset style="width:300px">
<legend>Registation Form</legend>
<table>
<tr>
<input type="text"
placeholder="firstname" maxlength="10">
</tr>
<br><br>
<tr>
<input type="text"
placeholder="lastname" maxlength="10">
</tr>
<br><br>
<tr>
<input type="email"
placeholder="[email protected]" id="e">
</tr>
<br><br>
<tr>
<input type="password"
placeholder="password">
</tr>
<br><br>
<tr>
<input type="password"
placeholder="confirm">
</tr>
<br><br>
<br><br>
<tr><input type="submit"
onclick="validate()" value="create">
</tr>
</table>
</fieldset>
</form>
</center>
</body>
</html>
Output :
Conclusion :