forked from pi-apps/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Url validation
59 lines (47 loc) · 1.54 KB
/
Url validation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from urllib.parse import urlparse
def is_valid_url(url):
parsed_url = urlparse(url)
return bool(parsed_url.scheme and parsed_url.netloc)
# Example usage
url = "https://www.paywithpi.com/path?query=value"
print("Valid URL!" if is_valid_url(url) else "Invalid URL!")
npm install validator
const validator = require('validator');
function isValidUrl(url) {
return validator.isURL(url);
}
// Example usage
const url = "https://www.paywithpi.com/path?query=value";
console.log(isValidUrl(url) ? "Valid URL!" : "Invalid URL!");
<input type="text" id="urlInput" placeholder="Enter URL">
<button onclick="validateUrl()">Validate URL</button>
<p id="result"></p>
<script>
function validateUrl() {
const url = document.getElementById('urlInput').value;
fetch('/validate-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url })
})
.then(response => response.json())
.then(data => {
document.getElementById('result').innerText = data.isValid ? "Valid URL!" : "Invalid URL!";
});
}
</script>
const express = require('express');
const bodyParser = require('body-parser');
const validator = require('validator');
const app = express();
app.use(bodyParser.json());
app.post('/validate-url', (req, res) => {
const { url } = req.body;
const isValid = validator.isURL(url);
res.json({ isValid });
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});