PrepAway - Latest Free Exam Questions & Answers

You develop a webpage by using HTML5. You create the following markup and code: (Line numbers are included for

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) ina HTML form. You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters. –

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 (/^[A-Za-z]+$/) which allows onlyletters. Next the match() method of string object is used to match the said regular expression against the input value.

Reference: JavaScript : HTML Form validation – checking for all letters


Leave a Reply