0% found this document useful (0 votes)
73 views5 pages

Web Practice Paper Form Validation PDF

The document contains practice questions and solutions for validating HTML form inputs. Some examples include: 1) Creating an input that only allows 3 letters with no numbers or special characters. 2) Creating a password input that requires a minimum of 6 characters. 3) Creating a password input that requires a minimum of 8 characters including at least one number, uppercase and lowercase letter. 4) Applying validation to a form to check that the name field is not empty and password length is at least 6 characters.

Uploaded by

Somya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views5 pages

Web Practice Paper Form Validation PDF

The document contains practice questions and solutions for validating HTML form inputs. Some examples include: 1) Creating an input that only allows 3 letters with no numbers or special characters. 2) Creating a password input that requires a minimum of 6 characters. 3) Creating a password input that requires a minimum of 8 characters including at least one number, uppercase and lowercase letter. 4) Applying validation to a form to check that the name field is not empty and password length is at least 6 characters.

Uploaded by

Somya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practice Questions

Form Validation

Question

Create an HTML form with an input field that can contain only three letters (no numbers or
special characters)

Solution

<html>

<body>

<form action="/action_page.php">

Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}">

<input type="submit">

</form>

</body>

</html>

Question

Create an <input> element with type="password" that must contain 6 or more characters

Solution

<body>

<form action="/action_page.php">
Password: <input type="password" name="pw" pattern=".{6,}" >
<input type="submit">
</form> </body>
Question:

Create an <input> element with type="password" that must contain 8 or more characters that are
of at least one number, and one uppercase and lowercase letter.

Solution

<html>

<body>

<form action="/action_page.php">

Password: <input type="password" name="pw" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}">

<input type="submit">

</form>

</body>

</html>

Question

Create a form and apply validation such that the name field can’t be empty and password can’t
be less than 6 characters long.The user will not be forwarded to the next page until given values
are correct.

Solution

<script>
function func(){
var name=document.myform.name.value;
var password=document.myform.password.value;

if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" action="abc.jsp" onsubmit="return func()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>

Question

Write a program for checking whether same password is entered in the fields “Enter Password”
and “Re-enter password field”

If value of password in both fields is not same then generate alert message “Passwords must be
same”

Solution

<script type="text/javascript">
function matchpass(){
var firstpassword=document.f1.password.value;
var secondpassword=document.f1.password2.value;

if(firstpassword==secondpassword)
{
alert("password are same!");

return true;
}
else{
alert("password must be same!");
return false;
}
}
</script>

<form name="f1" action="register.jsp" onsubmit="return matchpass()">


Password:<input type="password" name="password" /><br/>
Re-enter Password:<input type="password" name="password2"/><br/>
<input type="submit">
</form>

Question

Validate the textfield for numeric value only (using isNaN() function)

Solution
<script>
function validate(){
var num=document.myform.num.value;
if (isNaN(num)){
document.getElementById("numloc").innerHTML="Enter Numeric value only";
return false;
}else{
return true;
}
}
</script>
<form name="myform" onsubmit="return validate()" >
Number: <input type="text" name="num"><span id="numloc"></span><br/>
<input type="submit" value="submit">
</form>

Question
EMAIL VALIDATION
o email id must contain the @ and . character
o There must be at least one character before and after the @.
o There must be at least two characters after . (dot).
Solution

<script>
function f1()
{
var x=document.myform.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)
{
alert("Please enter a valid e-mail address”);
return false;
}
}
</script>
<body>
<form name="myform" action="#" onsubmit="return f1();">
Email: <input type="text" name="email"><br/>

<input type="submit" value="register">


</form>

You might also like