Latest Post:
Loading...

FORM VALIDATION (HTML+ CSS+ JAVASCRIPT)



FORM VALIDATION 


<!DOCTYPE html>
<html lang="en">
<head>
    <title>Form Validation</title>
    <!-- CSS START -->
    <style>
        /* Page background and font */
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
        }
        /* Form box styling */
        form {
            width: 350px;
            margin: 80px auto;          /* Center the form */
            background-color: #ffffff;
            padding: 25px;
            border-radius: 8px;         /* Rounded corners */
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        /* Label styling */
        label {
            font-weight: bold;
        }
        /* Input fields styling */
        input[type="text"],
        input[type="email"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
            box-sizing: border-box;
        }
        /* Submit button styling */
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
       /* Hover effect for submit button */
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
    <!-- CSS END -->
    <!-- JavaScript Validation -->
    <script>
        function validate()
        {
            // Check if name is empty
            if (document.myForm.txtbox.value == "")
            {
                alert("Please provide your name");
                document.myForm.txtbox.focus();
                return false;
            }
            // Check if email is empty
            if (document.myForm.emailid.value == "")
            {
                alert("Please provide your email!");
                document.myForm.emailid.focus();
                return false;
            }
            // Check if password is empty
            if (document.myForm.password.value == "")
            {
                alert("Please provide your password!");
                document.myForm.password.focus();
                return false;
            }
            // Check if retype password is empty
            if (document.myForm.repassword.value == "")
            {
                alert("Please retype your password!");
                document.myForm.repassword.focus();
                return false;
            }
            // Check if passwords match
            if (document.myForm.password.value != document.myForm.repassword.value)
            {
                alert("Passwords do not match!");
                document.myForm.repassword.focus();
                return false;
            }
            // If all validations pass
            return true;
        }
    </script>
</head>
<body>
    <!-- Registration Form -->
    <form name="myForm" onsubmit="return validate()">
        <!-- Name Field -->
        <label>Name:</label><br>
        <input type="text" name="txtbox"><br><br>
        <!-- Email Field -->
        <label>Email:</label><br>
        <input type="email" name="emailid"><br><br>
        <!-- Password Field -->
        <label>Password:</label><br>
        <input type="password" name="password"><br><br>
        <!-- Retype Password Field -->
        <label>Retype Password:</label><br>
        <input type="password" name="repassword"><br><br>
        <!-- Submit Button -->
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Post a Comment