levantermd commited on
Commit
bf2b226
·
verified ·
1 Parent(s): 78958c5

Create validation.js

Browse files
Files changed (1) hide show
  1. validation.js +37 -0
validation.js ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const PhoneNumber = require('awesome-phonenumber');
3
+ const cors = require('cors');
4
+
5
+ const app = express();
6
+ app.use(cors());
7
+ app.use(express.json());
8
+
9
+ // Validation endpoint
10
+ app.post('/validate', (req, res) => {
11
+ const { number } = req.body;
12
+
13
+ if (!number) {
14
+ return res.status(400).json({
15
+ valid: false,
16
+ message: 'Phone number is required.',
17
+ });
18
+ }
19
+
20
+ const pn = new PhoneNumber(number);
21
+ if (pn.isValid()) {
22
+ return res.json({
23
+ valid: true,
24
+ formatted: pn.getNumber('international'),
25
+ });
26
+ } else {
27
+ return res.json({
28
+ valid: false,
29
+ message: 'Invalid phone number. Ensure it starts with "+" and includes the correct country code.',
30
+ });
31
+ }
32
+ });
33
+
34
+ const PORT = process.env.PORT || 4000; // Use a different port to avoid conflict
35
+ app.listen(PORT, () => {
36
+ console.log(`Validation service running on port ${PORT}`);
37
+ });