Commit a77c6550 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add url check

parent f1cdd9f5
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<script src="./playlist.js" type="text/javascript"></script> <script src="./playlist.js" type="text/javascript"></script>
<body> <body>
<input type="text" class="inputIp" placeholder="input ip: http://xxx.xxx.xxx.xxx:port/(xxx is between 0 ~ 255)"/> <input type="text" class="inputIp" placeholder="input ip: http://xxx.xxx.xxx.xxx:port/(xxx is between 0 ~ 255) or url"/>
<input type="search" class="research" placeholder="research..."/> <input type="search" class="research" placeholder="research..."/>
......
/*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars, /*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars,
loopEventListener, RegExp, alert, promiseEventListener */ loopEventListener, RegExp, alert, promiseEventListener */
/*jslint maxlen:180, nomen: true */ /*jslint maxlen:180, nomen: true, regexp: true */
(function (window, rJS, $, Handlebars, loopEventListener) { (function (window, rJS, $, Handlebars, loopEventListener) {
...@@ -10,13 +10,21 @@ ...@@ -10,13 +10,21 @@
.getElementById('network').innerHTML, .getElementById('network').innerHTML,
network = Handlebars.compile(network_source); network = Handlebars.compile(network_source);
function endWith(str, end) {
return (str.indexOf(end, str.length - end.length) !== -1);
}
function contain(str, s) {
return (str.indexOf(s) !== -1);
}
function checkUrl(url) {
return (contain(url, ".com") || contain(url, ".net") || contain(url, ".fr"));
}
function checkIp(ip) { function checkIp(ip) {
var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/; var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (!re.test(ip)) { return re.test(ip);
return false;
}
return true;
} }
function check(value) { function check(value) {
...@@ -27,11 +35,17 @@ ...@@ -27,11 +35,17 @@
portEnd, portEnd,
ipValue = value; ipValue = value;
g.__element.getElementsByTagName('ul')[0].innerHTML = " "; g.__element.getElementsByTagName('ul')[0].innerHTML = " ";
if (ipValue.indexOf("/", ipValue.length - 1) === -1) { http = ipValue.indexOf("http");
info.innerHTML = " not end with /"; if (ipValue.indexOf("www.") !== -1) {
if (http === -1) {
info.innerHTML = "please start with http:// or https://";
return;
}
if (!checkUrl(ipValue)) {
info.innerHTML = "url invalide";
return; return;
} }
http = ipValue.indexOf("http://"); } else {
ipValue = ipValue.substring(ipValue.indexOf("//") + 2); ipValue = ipValue.substring(ipValue.indexOf("//") + 2);
port = ipValue.indexOf(":"); port = ipValue.indexOf(":");
portEnd = ipValue.indexOf(":/"); portEnd = ipValue.indexOf(":/");
...@@ -39,18 +53,23 @@ ...@@ -39,18 +53,23 @@
ipValue = ipValue.substring(0, port); ipValue = ipValue.substring(0, port);
} }
if (http === -1) { if (http === -1) {
info.innerHTML = " please start ip with http://"; info.innerHTML = "please start ip with http:// or https://";
return;
}
if (checkIp(ipValue) === false) {
info.innerHTML =
"invalide ip: ip should like xxx.xxx.xxx.xxx(xxx is between 0 ~ 255)";
return; return;
} }
if (port === -1 || portEnd !== -1) { if (port === -1 || portEnd !== -1) {
info.innerHTML = "input port number"; info.innerHTML = "input port number";
return; return;
} }
if (checkIp(ipValue) === false) { if (!endWith(value, "/")) {
info.innerHTML = info.innerHTML = "not end with /";
"invalide ip: ip should like xxx.xxx.xxx.xxx(xxx is between 0 ~ 255)";
return; return;
} }
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return g.plSave({"ip": value}); return g.plSave({"ip": value});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment