Web Practice Paper Form Validation PDF
Web Practice Paper Form Validation PDF
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">
<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">
<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>
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/>