Skip to content

Commit f5bd3e2

Browse files
committed
Validate Email and Validate Pattern Rules added
1 parent 384d8f4 commit f5bd3e2

File tree

4 files changed

+672
-22
lines changed

4 files changed

+672
-22
lines changed

README.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- [Usage](#usage)
1414
- [Validate Required Inputs](#validate-required-inputs)
1515
- [Validate Min & Max](#validate-min-and-max)
16+
- [Validate Email](#validate-email)
17+
- [Validate Pattern](#validate-pattern)
1618
- [Example Reactjs Code](#example-reactjs-code)
1719
- [Licence](#license)
1820
## Installation
@@ -57,7 +59,7 @@ Then, wrap your form with <ValidateForm> :
5759

5860
```
5961
# Rules
60-
## Validate Required Inputs
62+
# Validate Required Inputs
6163

6264
#### To check if all required input fields are filled, use the following rule:
6365

@@ -91,7 +93,7 @@ If a required input is not filled, the rule will return a callback with an array
9193

9294

9395
#
94-
## Validate Min and Max
96+
# Validate Min and Max
9597

9698
#### Checking all **Min** and **Max** values of a form inputs and returning a callback and show error.
9799

@@ -138,6 +140,105 @@ the `max` in message object is when exceeded maximum the message will show
138140
| `exceedsMin` | `callback function` | when exceeded min | `yes` |
139141
| `onsuccess` | `callback function` | validatedInput | `yes` |
140142

143+
#
144+
# Validate Email
145+
146+
#### This is used to validate email addresses based on pre-defined patterns. It takes input from a form and checks it against a regular expression pattern
147+
148+
```javascript
149+
ValidateEmail: {
150+
151+
type: "yahoo",
152+
emailInput: "my_email",
153+
message: "Please enter a valid yahoo email",
154+
onsuccess: () => console.log("Email is valid"),
155+
invalid: () => console.log("Email is invalid"),
156+
when: "onblur",
157+
158+
}
159+
```
160+
161+
```html
162+
<input name="email" type="my_email" required />
163+
```
164+
165+
## Parameters
166+
167+
#### `type:` a string representing the type of email being validated (personal, business, yahoo, gmail, hotmail, aol, isp, education, government, nonprofit, international, domain-specific, or alias)
168+
#### `emailInput:` a string representing the name of the input element in the form that contains the email address
169+
#### `message:` a string representing the error message to be displayed if the email is invalid
170+
#### `onsuccess:` a callback function to be executed if the email is valid
171+
#### `invalid:` a callback function to be executed if the email is invalid
172+
#### `when:` a string specifying when the validation should occur (onblur or typing)
173+
#
174+
# Validate Pattern
175+
176+
#### This function is used to validate input fields against a specific pattern, such as a regular expression or a wildcard pattern.
177+
178+
179+
```javascript
180+
validatePattern:{
181+
182+
input: 'email',
183+
pattern: /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/,
184+
type: 'regex',
185+
when: 'typing',
186+
allowEmpty: false,
187+
onsuccess: (inputElement) => console.log('Validation succeeded!’),
188+
invalid: () => console.log('Validation failed!’),
189+
errorMessage: 'Please enter a valid email address.'
190+
191+
}
192+
```
193+
194+
#### Validate using a regular expression pattern
195+
196+
```javascript
197+
validatePattern:{
198+
pattern: /^\S+@\S+\.\S+$/,
199+
modifiers: 'i',
200+
errorMessage: 'Please enter a valid email address',
201+
}
202+
```
203+
204+
#### Validate using a wildcard pattern
205+
206+
```javascript
207+
validatePattern:{
208+
pattern: '*.com',
209+
type: 'wildcard',
210+
modifiers: 'i',
211+
errorMessage: 'Please enter an email address ending in .com'
212+
}
213+
```
214+
215+
#### Allow empty input
216+
217+
```javascript
218+
validatePattern:{
219+
pattern: /^\S+@\S+\.\S+$/,
220+
modifiers: 'i',
221+
allowEmpty: true,
222+
}
223+
```
224+
225+
## Parameters
226+
227+
* #### `input` (required): The name of the input field to validate.
228+
* #### `pattern` (required): A string representing the pattern to validate against. It can be a regular expression or a wildcard pattern.
229+
* #### `type` (optional): A string representing the type of pattern used. It can be either `"regex"` or `"wildcard"`. If not specified, `"regex"` is used by default.
230+
* #### `modifiers` (optional): A string representing the modifiers for the regular expression. Only used when `type` is `"regex"`.
231+
* #### `when` (optional): A string representing the event that triggers the validation. It can be either `"onblur"` or `"typing"`. If not specified, `"onblur"` is used by default.
232+
* #### `allowEmpty` (optional): A boolean value indicating whether empty input should be considered valid. If not specified, `false` is used by default.
233+
* #### `onsuccess` (optional): A function that is called when the validation succeeds.
234+
* #### `invalid` (optional): A function that is called when the validation fails.
235+
* #### `errorMessage` (optional): A string representing the error message to display when the validation fails.
236+
237+
238+
We provide two different patterns - a regular expression pattern and a wildcard pattern - along with options for case-insensitivity (`modifiers: 'i'`) and custom error messages.
239+
240+
We also use the `allowEmpty` option to allow the input to be empty, which can be useful for optional fields.
241+
141242
#
142243
# Example Reactjs Code
143244

src/index.js

Lines changed: 243 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)