You develop a webpage by using HTML5. You create the following markup and code: (Line numbers
are included for reference only.)
You need to ensure that the values that users enter are only numbers, letters, and underscores,
regardless of the order.
Which code segment should you insert at line 04?
A.
Option A
B.
Option B
C.
Option C
D.
Option D
Explanation:
Example:
Sometimes situations arise when user should fill a single or more than one fields with alphabet
characters (A-Z or a-z) in a HTML form. You can write a JavaScript form validation script to check
whether the required field(s) in the HTML form contains only letters. – See more at:
http://www.w3resource.com/javascript/form/all-letters-field.php#sthash.5Db50Bdm.dpuf
Javascript function to check for all letters in a field
view plainprint?
function allLetter(inputtxt)
{
var letters = /^[A-Za-z]+$/;
if(inputtxt.value.match(letters))
{
return true;
}
else
{
alert("message");
return false;
}
}
To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[AZa-z]+$/) which allows only letters. Next the match() method of string object is used to match the
said regular expression against the input value. Here is the complete web document.
– See more at: http://www.w3resource.com/javascript/form/all-lettersfield.php#sthash.5Db50Bdm.dpuf
Reading the explanation that comes with the answer, it seems to be explaining why option C is the correct answer, yet the correct answer is indicated to be option A.
Which is correct, A or C? They both look OK to me…
Try it here. Option A works and option C fails. https://jsfiddle.net/8pLxL6rq/
Option A works – https://jsfiddle.net/4atxsk2a/