The screenshot at right is the initial rendering of the HTML document prob2.html
ID: 3575870 • Letter: T
Question
The screenshot at right is the initial rendering of the HTML document prob2.html, and it is listed below. When it finishes loading, it calls function start (), which is defined in file prob2.js, which prob2.html references in its head. Final, Problem 2 Your code: Function start() makes function validate (), which is also defined in prob2.js, the handler for submit events for this form element. Function validate() uses a regular expression to check that the value in the textbox has the following pattern: capital letter A or B, then exactly two digits, then one of the capital letters U-Z. This is all that may appear in the textbox (use the anchors^and $). The match remembers the two digits (they are a captured group). If the string that is the content of the textbox fails to match this pattern, the function raises an alert box as shown at right above and cancels the from submission. If the string matches the pattern but the digits do not represent a number between 25 and 74 inclusive, the function raises an alert box as shown at right below and again cancels the form submission. (If the string matches and the digits represent a number that is in range, then this function does nothing: the submission goes through.) On the next page, write the contents of prob2.js, that is, the functions start () and validate(). (My version of validate () has 10 lines, not counting blank lines and lines with only brackets.)Explanation / Answer
prob2.js
-------------------
function validate()
{
var str = document.getElementById("inp").value;
var pat1 =/[AB]/g;
var pat2 =/[U-Z]/g;
var pat3=/d+/g;
var m=str.match(pat3);
var l=Math.max(...m);
var vl=str.match(pat3);
var n=parseInt(vl);
if(!str.match(pat1))
{
alert("III-formed code");
return false;
}
else if(!str.match(pat2))
{
alert("III-formed code");
return false;
}
else if(l<25||l>74)
{
alert("Numeric value out of range");
return false;
}
else
return true;
}
------------prob2.html-----------
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Final Problem2</title>
<script type="text/javascript" src="prob2.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="prob2.php" method="get">
<p>
Your code:
<input type="text" id="inp" name="cd" size="6"/>
</p>
<p><input type="submit" value="submit"/></p>
</form>
</body>
</html>
//comment if any doubhts