Unit-5 Regular Expressions Vikas Patil
RegEx
W-19
Q.4 (a) Describe regular expression. Explain search () method used in regular expression with suitable example. (4M)
<html>
<head>
<title>Search method in Regex</title>
<script>
function Search(){
var str="Good Morning!";
var re1=str.search(/Morning/i);
document.write(re1+"<br>");
var re2=str.search(/morning/);
document.write(re2);
}
</script>
</head>
<body>
<button onclick="Search()">Search </button>
</body>
</html>
W-22
Q. Write a JavaScript function to check the first character of a string is uppercase or not.
<html>
<head>
<title>Check 1st alphabet Uppercase or not</title>
<script>
function checkUpperCase(){
var str=prompt("Enter a string ");
var regUpper=/^[A-Z]/;
if(regUpper.test(str)){
alert ("Is in upper case");
}else{
alert ("Not in uper case");
}
}
checkUpperCase();
</script>
</head>
</html>
S-22
Q.2 (d) State what is regular expression. Explain its meaning with the help of a suitable example. (4M)
Q. Write a javascript program to validate email ID of the user using regular expression. (4M)
Ans. Use this expression /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ to check.
1
Unit-5 Regular Expressions Vikas Patil
S-23
Q.4 (a) Explain test() and exec() method of Regular Expression object with example. (4M)
Q.5 (c) Write HTML code to design a form that displays textboxes for accepting UserID and Aadhar No. and a SUBMIT
button. UserID should contain 10 alphanumeric characters and must start with Capital Letter. Aadhar No. should contain
12 digits in the format nnnn nnnn nnnn. Write JavaScript code to validate the UserID and Aadhar No. when theuser clicks
on SUBMIT button. (6M)
<html>
<head>
<title>Validate user id and aadhar no</title>
<script>
function submitData(){
var uid=document.getElementById("uid").value;
var aadhar=document.getElementById("aadhar").value;var regId=/^[A-
Z][A-Za-z0-9]{9}$/;
var regAadhar=/^\d{4}-\d{4}-\d{4}$/;
if(!regId.test(uid)){
alert("Invalid User ID . Must contain 10 alphanumeris charactersStarting with capital letter");
}else if(!regAadhar.test(aadhar)){
alert("Invalid Aadhar NO. . It should be in nnnn-nnnn-nnnn format");
}else{
alert("Valid User");
}
</script>
</head>
W-23 <body>
Q.4 (a) Write a<form name="f1">
JavaScript that accepts a string and searches for the pattern “MSBTE” in the given string using regular
expressions. If the pattern is found, JavaScript
<input type="text" will display that Your
id="uid" placeholder="Enter “Pattern is "found”
User ID else display “Pattern is not found”.
required><br><br>
(4M)
<input type="text" id="aadhar" placeholder="Enter your Aadhar No."required><br><br>
<html>
<head> <input type="button" id="b1" value="Submit" onclick="submitData()">
<title>regex</title>
</form>
</head>
<body>
</body>
<script type="text/javascript">
</html> function check()
{
var str=prompt("Enter String :");
var pattern=/MSBTE/g;
2
Unit-5 Regular Expressions Vikas Patil
if(pattern.test(str))
{
alert("MSBTE found in String");
}
else
{
alert("MSBTE not Found");
}
}
check();
</script>
</body>
</html>
Q.5 (b) Form regular expressions for following : (6M)
(i) Validation of email address.
Ans. ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
(ii) Validation of adhaar card. Format is dddd – dddd – dddd
Ans. ^\d{4}-\d{4}-\d{4}$
(iii) Validation of phone number. Format is (ddd) – (dddddddd)
Ans. ^\(\d{3}\)-\(\d{8}\)$