0% found this document useful (0 votes)
10 views2 pages

Multiple Form Validation

The document contains a JavaScript function for validating form inputs, specifically checking if the 'name' field is empty and if the 'email' field has a valid format. It includes two HTML forms that utilize this validation function on submission. Alerts are triggered for validation errors, and the function returns a boolean indicating the validity of the form inputs.

Uploaded by

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

Multiple Form Validation

The document contains a JavaScript function for validating form inputs, specifically checking if the 'name' field is empty and if the 'email' field has a valid format. It includes two HTML forms that utilize this validation function on submission. Alerts are triggered for validation errors, and the function returns a boolean indicating the validity of the form inputs.

Uploaded by

Bacha Sirata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

// script.

js

function validateForm(form) {

let isValid = true;

// Example: Validate a text input named 'name'

const nameInput = [Link]('input[name="name"]');

if (nameInput && [Link]() === '') {

alert('Name field cannot be empty in ' + [Link]);

isValid = false;

// Example: Validate an email input named 'email'

const emailInput = [Link]('input[name="email"]');

if (emailInput && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test([Link])) {

alert('Please enter a valid email in ' + [Link]);

isValid = false;

// Add more validation rules as needed for different field types and requirements

return isValid;

}
<!-- [Link] -->

<form id="form1" onsubmit="return validateForm(this);">

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

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

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

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

<button type="submit">Submit Form 1</button>

</form>

<form id="form2" onsubmit="return validateForm(this);">

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

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

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

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

<button type="submit">Submit Form 2</button>

</form>

<script src="[Link]"></script>

You might also like