Skip to content

Commit 43cac96

Browse files
committed
Validation Functions
1 parent c9ae43c commit 43cac96

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Validate a Credit Card Number
3+
description: Validates a Credit Card Number for a variety of major international credit card formats, including Visa, MasterCard, American Expres, Diners Club and JCB.
4+
author: JanluOfficial
5+
tags: validation,url
6+
---
7+
8+
```js
9+
function validateCreditCardNumber(cardNumber) {
10+
const cardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$/;
11+
return cardRegex.test(cardNumber);
12+
}
13+
14+
// Usage:
15+
validateCreditCardNumber("4111111111111111"); // Returns: true
16+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Validate a URL
3+
description: Validates a URL.
4+
author: JanluOfficial
5+
tags: validation,url
6+
---
7+
8+
```js
9+
function validateURL(url) {
10+
const urlRegex = /^(https?|http?|ftp):\/\/[^\s/$.?#].[^\s]*$/i;
11+
return urlRegex.test(url);
12+
}
13+
14+
// Usage:
15+
validateURL("https://www.example.com"); // Returns: true
16+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Validate a US Phone Number
3+
description: Validates a phone number from the United States of America.
4+
author: JanluOfficial
5+
tags: validation,phone,united-states
6+
---
7+
8+
```js
9+
function validateUSPhoneNumber(phoneNumber) {
10+
const phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
11+
return phoneRegex.test(phoneNumber);
12+
}
13+
14+
// Usage:
15+
validateUSPhoneNumber("123-456-7890"); // Returns: true
16+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Validate an Email Adress
3+
description: Validates an Email Adress
4+
author: JanluOfficial
5+
tags: validation,email
6+
---
7+
8+
```js
9+
function validateEmail(email) {
10+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
11+
return emailRegex.test(email);
12+
}
13+
14+
// Usage:
15+
validateEmail("[email protected]"); // Returns: true
16+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Validate an international Phone Number
3+
description: Validates an international Phone Number.
4+
author: JanluOfficial
5+
tags: validation,phone,international
6+
---
7+
8+
```js
9+
function validateInternationalPhoneNumber(phoneNumber) {
10+
const phoneRegex = /^\+?[1-9]\d{1,14}$/;
11+
return phoneRegex.test(phoneNumber);
12+
}
13+
14+
// Usage:
15+
validateInternationalPhoneNumber("+1234567890"); // Returns: true
16+
```

0 commit comments

Comments
 (0)