0% found this document useful (0 votes)
7 views20 pages

week3

The document contains HTML code for three forms: a registration form, a login form, and a catalog search form, each with client-side JavaScript validation. The registration form checks for valid name, email, and phone number formats, while the login form validates email and password requirements. The catalog form ensures that category selection is made, and that price inputs are valid numbers, with additional checks for minimum and maximum price constraints.

Uploaded by

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

week3

The document contains HTML code for three forms: a registration form, a login form, and a catalog search form, each with client-side JavaScript validation. The registration form checks for valid name, email, and phone number formats, while the login form validates email and password requirements. The catalog form ensures that category selection is made, and that price inputs are valid numbers, with additional checks for minimum and maximum price constraints.

Uploaded by

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

Register.

html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;


if (!phone.match(phonePattern)) {

alert("Please enter a valid phone number (10 digits)");

return false;

// If everything is fine, allow form submission

return true;

</script>

</head>

<body>

<h2>Client-side Form Validation</h2>

<form onsubmit="return validateForm()">

<label for="name">Name:</label>

<input type="text" id="name" name="name">

<br><br>

<label for="email">Email:</label>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;


var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;


// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;


// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)


if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {
alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");


return false; // Prevent form submission

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission


}

// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

}
// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

}
// Validate email (basic email format)

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Form Validation Example</title>

<script>

function validateForm() {

// Get the values of the form fields

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var phone = document.getElementById("phone").value;

// Validate name (it should not be empty)

if (name == "") {

alert("Name must be filled out");

return false; // Prevent form submission

// Validate email (basic email format)


var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (!email.match(emailPattern)) {

alert("Please enter a valid email address");

return false;

// Validate phone number (basic phone format)

var phonePattern = /^[0-9]{10}$/;

<input type="email" id="email" name="email">

<br><br>

<label for="phone">Phone (10 digits):</label>

<input type="text" id="phone" name="phone">

<br><br>

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

</form>

</body>

</html>

Login.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Login Form Validation</title>

<script>

function validateLoginForm() {

// Get the values of the form fields


var email = document.getElementById("email").value;

var password = document.getElementById("password").value;

// Email validation - check if the email field is not empty and matches a valid email format

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

if (email === "") {

alert("Email is required.");

return false; // Prevent form submission

} else if (!email.match(emailPattern)) {

alert("Please enter a valid email address.");

return false; // Prevent form submission

// Password validation - check if the password field is not empty and meets minimum length

if (password === "") {

alert("Password is required.");

return false; // Prevent form submission

} else if (password.length < 6) {

alert("Password must be at least 6 characters long.");

return false; // Prevent form submission

// If all validations pass, allow the form to be submitted

return true;

</script>

</head>

<body>

<h2>Login Page</h2>
<form onsubmit="return validateLoginForm()">

<label for="email">Email:</label>

<input type="email" id="email" name="email" placeholder="Enter your email" required>

<br><br>

<label for="password">Password:</label>

<input type="password" id="password" name="password" placeholder="Enter your password"


required>

<br><br>

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

</form>

</body>

</html>

Catalog.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Catalog Page - Client-Side Validation</title>

<script>

function validateCatalogForm() {

// Get the values of the form fields

var category = document.getElementById("category").value;

var minPrice = document.getElementById("minPrice").value;

var maxPrice = document.getElementById("maxPrice").value;

var keyword = document.getElementById("keyword").value;


// Validate category (it should not be empty)

if (category === "") {

alert("Please select a category.");

return false; // Prevent form submission

// Validate minPrice and maxPrice (both should be numbers and minPrice should not be greater
than maxPrice)

if (minPrice !== "" && isNaN(minPrice)) {

alert("Please enter a valid minimum price.");

return false; // Prevent form submission

if (maxPrice !== "" && isNaN(maxPrice)) {

alert("Please enter a valid maximum price.");

return false; // Prevent form submission

if (minPrice !== "" && maxPrice !== "" && parseFloat(minPrice) > parseFloat(maxPrice)) {

alert("Minimum price cannot be greater than maximum price.");

return false; // Prevent form submission

// Validate keyword (optional - just checking if it's empty or not)

if (keyword.length > 0 && keyword.length < 3) {

alert("Keyword must be at least 3 characters long.");

return false; // Prevent form submission

// If everything is valid, allow form submission

return true;
}

</script>

</head>

<body>

<h2>Catalog Search</h2>

<form onsubmit="return validateCatalogForm()">

<label for="category">Category:</label>

<select id="category" name="category">

<option value="">Select a category</option>

<option value="electronics">Electronics</option>

<option value="books">Books</option>

<option value="clothing">Clothing</option>

</select>

<br><br>

<label for="minPrice">Min Price:</label>

<input type="text" id="minPrice" name="minPrice" placeholder="Enter minimum price">

<br><br>

<label for="maxPrice">Max Price:</label>

<input type="text" id="maxPrice" name="maxPrice" placeholder="Enter maximum price">

<br><br>

<label for="keyword">Search Keyword:</label>

<input type="text" id="keyword" name="keyword" placeholder="Enter keyword (optional)">

<br><br>

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

</form>
</body>

</html>

Cart.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Shopping Cart - Client-Side Validation</title>

<script>

function validateCartForm() {

// Get the values of the form fields

var quantities = document.getElementsByClassName("quantity");

var isValid = true;

var invalidFields = [];

// Validate that each quantity is a valid positive number

for (var i = 0; i < quantities.length; i++) {

var quantity = quantities[i].value;

if (quantity === "" || isNaN(quantity) || quantity <= 0) {

invalidFields.push("Quantity for item " + (i + 1));

isValid = false;

// Validate coupon code (optional validation, e.g., must be at least 5 characters)

var couponCode = document.getElementById("coupon").value;

if (couponCode.length > 0 && couponCode.length < 5) {


alert("Coupon code must be at least 5 characters long.");

isValid = false;

// Validate delivery address fields (basic check for empty fields)

var address = document.getElementById("address").value;

var city = document.getElementById("city").value;

var postalCode = document.getElementById("postalCode").value;

if (address === "" || city === "" || postalCode === "") {

alert("Please fill in all the address fields.");

isValid = false;

// If there are any invalid fields, show an alert

if (!isValid) {

alert("Please correct the following issues: \n- " + invalidFields.join("\n- "));

return false; // Prevent form submission

// If everything is valid, allow form submission

return true;

</script>

</head>

<body>

<h2>Shopping Cart</h2>

<form onsubmit="return validateCartForm()">


<!-- Example product items -->

<div class="cart-item">

<label for="item1">Item 1 (Product A)</label>

<input type="number" id="item1" class="quantity" name="item1" value="1" min="1">

<br><br>

</div>

<div class="cart-item">

<label for="item2">Item 2 (Product B)</label>

<input type="number" id="item2" class="quantity" name="item2" value="2" min="1">

<br><br>

</div>

<!-- Coupon code input -->

<label for="coupon">Coupon Code (optional):</label>

<input type="text" id="coupon" name="coupon" placeholder="Enter coupon code">

<br><br>

<!-- Delivery address fields -->

<label for="address">Address:</label>

<input type="text" id="address" name="address" placeholder="Enter delivery address">

<br><br>

<label for="city">City:</label>

<input type="text" id="city" name="city" placeholder="Enter city">

<br><br>

<label for="postalCode">Postal Code:</label>

<input type="text" id="postalCode" name="postalCode" placeholder="Enter postal code">


<br><br>

<!-- Submit button -->

<input type="submit" value="Proceed to Checkout">

</form>

</body>

</html>

You might also like