11
11
12
12
<!-- Sample expected answer -->
13
13
< script id ="expected0 " type ="plain/text ">
14
- query ( 'id' ) . isLength ( { max :80 } ) . matches ( / ^ [ A - Z ] { 2 } - [ 0 - 9 ] + - [ 0 - 9 ] + $ / ) ,
14
+ query ( 'id' ) . isLength ( { max :80 } ) .
15
+ matches ( / ^ [ A - Z ] { 2 } - [ 0 - 9 ] + - [ 0 - 9 ] + $ / ) ,
15
16
</ script >
16
17
17
18
<!-- Full pattern of correct answer.
78
79
text : >
79
80
You need to match one or more digits ; * allows 0 or more .
80
81
A + would be better suited for this task .
82
+ - present : |
83
+ \s * , , $
84
+ text : >
85
+ You have two commas at the end . Use only one . You may need to
86
+ scroll or increase the text area to see both of them .
81
87
successes :
82
88
- - query ( `id` ) . isLength ( { max :80 } ) . matches ( / ^ [ A - Z ] { 2 } - \d + - [ 0 - 9 ] + $ / ) ,
83
89
- - ' query (`id`) . isLength( {max:80}).matches(/^[A-Z]{2}-\d+-[0-9]+$/ ) , '
@@ -100,9 +106,10 @@ <h1>Lab Exercise input2</h1>
100
106
< p >
101
107
< h2 > Task</ h2 >
102
108
< p >
103
- < b > Please change the code below so the query parameter
104
- < tt > id</ tt > < i > must</ i > be no longer than 80 characters
105
- and meets this application's part id format requirement.</ b >
109
+ < b >
110
+ Practice validating specialized text input in a program
111
+ using a regular expression.
112
+ </ b >
106
113
107
114
< p >
108
115
< h2 > Background</ h2 >
@@ -150,17 +157,17 @@ <h2>Task Information</h2>
150
157
another dash (< tt > -</ tt > ), and another sequence of one or more digits.
151
158
152
159
< p >
153
- To do that,
154
- after the first parameter to < tt > app.get </ tt >
155
- which says < tt > '/parts' </ tt > ,
156
- add a new comma-separated parameter.
157
- Start this new parameter with
160
+ To do that:
161
+ < ol >
162
+ < li > After the first parameter to < tt > app.get </ tt >
163
+ which says < tt > '/parts' </ tt > , add a new comma-separated parameter.
164
+ < li > Start this new parameter with
158
165
< tt > query('id')</ tt > to select the
159
- < tt > id</ tt > parameter for validation (we've have < i > not</ i > filled
166
+ < tt > id</ tt > parameter for validation (we have < i > not</ i > filled
160
167
in this part in this lab).
161
- Add a period (< tt > .</ tt > ) and the validation requirement
168
+ < li > Add a period (< tt > .</ tt > ) and the validation requirement
162
169
< tt > isLength()</ tt >
163
- The < tt > isLength</ tt > method takes, as an optional parameter inside
170
+ < li > The < tt > isLength</ tt > method takes, as an optional parameter inside
164
171
its parentheses,
165
172
an object providing specific information such as a minimum and a maximum.
166
173
We only want a maximum,
@@ -169,42 +176,55 @@ <h2>Task Information</h2>
169
176
Those familiar with JavaScript will know that the "length" value is
170
177
the length of the string in UTF-16 code units; that's fine
171
178
for our purposes.
179
+ </ ol >
172
180
</ p >
173
181
174
182
< p >
175
183
We also need to verify that the input matches our pattern,
176
184
which we can verify using a regular expression.
177
- In this situation, we can do this by appending another
178
- period (< tt > .</ tt > ) and the validation requirement
185
+ In this situation, we can do this by:
186
+ < ol >
187
+ < li > appending another period (< tt > .</ tt > ) and the validation requirement
179
188
< tt > matches()</ tt > .
180
- Inside those parenthesis you should supply slash (< tt > /</ tt > , the
189
+ < li > Inside those parenthesis you should supply slash (< tt > /</ tt > , the
181
190
text of the regular expression to match, and another slash (< tt > /</ tt > ).
182
191
In JavaScript, a pair of slashes (< tt > /</ tt > ) surrounds a regular expression.
183
- Remember to match the < i > entire</ i > expression
192
+ < li > Remember to match the < i > entire</ i > expression
184
193
(in other words, use < tt > ^</ tt > and < tt > $</ tt > ).
185
- Remember, a way to match a single uppercase character is the
186
- pattern < tt > [A-Z]</ tt > ... good luck!
194
+ < li > Also, remember that a way to match a single uppercase character is the
195
+ pattern < tt > [A-Z]</ tt >
196
+ </ ol >
187
197
188
198
< p >
189
199
< h2 > Interactive Lab (< span id ="grade "> </ span > )</ h2 >
190
200
< p >
201
+ < b > Please change the code below so the query parameter
202
+ < tt > id</ tt > is only accepted if it's no longer than 80 characters
203
+ and meets this application's part id format requirement.
204
+ The format is
205
+ two uppercase Latin letters (each < tt > A</ tt > through < tt > Z</ tt > ),
206
+ then a dash (< tt > -</ tt > ), a sequence of one or more digits,
207
+ another dash (< tt > -</ tt > ), and another sequence of one or more digits.
208
+ </ b >
209
+ < p >
210
+
191
211
<!--
192
212
You can use this an example for new labs.
193
213
For multi-line inputs, instead of <input id="attempt" type="text" ...>, use
194
214
<textarea id="attempt" rows="2" cols="65">...</textarea>
195
215
-->
196
216
< form id ="lab ">
197
- < pre > < code >
198
- // Set up Express framework and express-validator library
217
+ < pre > < code
218
+ > // Set up Express framework and express-validator library
199
219
const express = require("express");
200
220
const app = express();
201
221
const { query, matchedData, validationResult } =
202
222
require('express-validator');
203
223
204
224
// Implement requests, e.g., http://localhost:3000/parts?id=1
205
225
app.get('/parts',
206
- < input id ="attempt0 " type =" text " size =" 65 " spellcheck ="false "
207
- value =" , " >
226
+ < textarea id ="attempt0 " rows =" 2 " cols =" 64 " spellcheck ="false ">
227
+ , </ textarea >
208
228
(req, res) => { // Execute this code if /invoices seen
209
229
const result = validationResult(req); // Retrieve errors
210
230
if (result.isEmpty()) { // No errors
0 commit comments