Exp 12
Exp 12
12
Aim: Develop a web page for validation of form fields using regular expression.
Theory:
Regular expression is a feature of JavaScript that allow to specify patterns of characters to be search or
match with the content of variable or any information. It is used to validate data filled in form elements. A
regular expression may tell the browser to find whether a specific character or pattern exists in one or more
lines of text or to replace all concurrences of a word with another word,etc.
A regular expression tells the browser how to manipulate text using special symbols as operators. It begins
and ends with a slash(/). All special characters that are used to create a regular expression are placed
between the slashes. After the first slash , a pair of square bracket ( [ ] ) is used to tell the browser the text or
pattern of characters to be search.
Syntax: regexpvar = / [ search pattern ] / ;
- regexpvar is an object that stores search pattern
- / indicates starting of regular expression
- [ search pattern ] indicated character to be search
-/ indicates end of regular expression
Test method is used with regular expression object to check whether the pattern of characters exist in the
string or not.
Syntax: regexpvar.test(“string”/ variable name that contains string);
In JavaScript , a set of special characters is used to create a regular expression such as [] square bracket.
Special characters used to create a regular expression:
Special character Description
\ Tell the browser to ignore special meaning of the following character.
$ End of string
\D Any nondigit
{n,m} At least n but no more than m occurrences of the previous sub-pattern or character
set.
Matching words:
In some JavaScript application, one can search for a particular word within the text. A word is defined by a
word boundary. A word boundary is specified within a regular expression by using \b special symbol.
Two \b special symbols are used in search pattern to specify word boundary. First \b represents the space at
the beginning of the word and the second \b represents the space at the end of the word.
Programs:
Program to search for presence of any character between J-P in string. Returns True if series
character is present in string.
<html>
<head>
<title>Simple Regular Expression</title>
<script type="text/javascript">
function validate()
{
var name="welcome to HTML";
re = /[J-P]/;
if (re.test(name))
{
alert('series character is present’);
}
else
{
alert(‘series character is not present');
}
}
</script>
</head>
<body>
<form>
<p>
<input name="reg" value="test" type="button" onclick=" validate()"/>
</p>
</form>
</body>
</html>
Output:
<html>
<head>
<title>Simple Regular Expression</title>
<script type="text/javascript">
function validate()
{
with(document.forms.form1)
{
var name=form1.t1.value;
re = /\d/
if (re.test(name))
{
alert('InCorrect First Name');
}
else
{
alert('correct First Name');
}
}
}
</script>
</head>
<body>
<FORM name="form1">
<P>
Enter First Name: <input type="text" name="t1">
<INPUT name="reg" value="Submit" type="button" onclick="validate()"/>
</P>
</FORM>
</body>
</html>
Output:
Conclusion:
With all the concepts based on regular expression , successfully executed all programs with correct output.