An open API service indexing awesome lists of open source software.

https://github.com/codewithmark/one-line-validator

Validate Any Form in 60 Seconds (1 Line of Code!)
https://github.com/codewithmark/one-line-validator

Last synced: 8 days ago
JSON representation

Validate Any Form in 60 Seconds (1 Line of Code!)

Awesome Lists containing this project

README

          

# ๐Ÿงช OneLineValidator

**A lightweight, beginner-friendly JavaScript class for validating HTML forms with custom error messages and clean visual feedback.**

---

## ๐Ÿš€ Features

* โœ… One-line validation with `OneLineValidator.validate(...)`
* ๐ŸŽฏ Custom error messages via `id` or `class`
* ๐Ÿ”„ Live field validation on input/change
* โœจ Styled error messages with animation
* ๐Ÿ“ฆ Returns form data object when valid, `false` when not
* ๐Ÿ’ก Treats any field with a custom error as required (no `required` attribute needed)

---

## ๐Ÿ› ๏ธ Installation

Download or copy `OneLineValidator.js` into your project.

Then include it:

```html

```

Or import via module (if using bundlers like Vite/Webpack):

```js
import OneLineValidator from './OneLineValidator.js';
```

---

## โœ… Usage Example

### HTML Form

```html
IMPORTANT NOTE > all the input fields need to have id assigned to it otherwise return object will be null






Submit

```

### JavaScript

```js

//IMPORTANT NOTE > all the input fields need to have id assigned to it otherwise return object will be null
const customErrors = [
{ id: "email", msg: "Please enter a valid email address." },
{ class: "password", msg: "Password must be at least 6 characters." },
{ id: "confirmPassword", msg: "Passwords must match." },
{ class: "phone", msg: "Enter a valid phone number (10โ€“15 digits)." },
{ id: "terms", msg: "You must accept the terms to continue." }
];

document.querySelector('#myForm').addEventListener('submit', function (e) {
e.preventDefault();

const data = OneLineValidator.validate('#myForm', customErrors);

if (data) {
console.log('โœ… Form is valid!', data);
// Submit or use data
} else {
console.log('โŒ Form has errors');
}
});
```

---

## ๐Ÿ“ฆ Return Format (If Valid)

```json
{
"email": "user@example.com",
"password": "abc123",
"confirmPassword": "abc123",
"phone": "+1234567890",
"terms": true
}
```

---

## ๐ŸŽจ Styling

Styles are automatically injected:

* `.field-error` adds a red border and light red background
* `.error-message` shows an animated message with an โŒ icon

---

## ๐Ÿ“š License

MIT โ€” free to use, modify, and share.

---

## ๐Ÿ’ฌ Author

Built by \[Code With Mark].
Contributions, suggestions, and issues welcome!