如何检查文本框JavaScript中的链接

时间:2013-12-23 12:04:12

标签: javascript html

如果文本框中有链接,则JavaScript会在按下按钮时将用户重定向到链接。例如,如果文本框中包含www.google.com,则JavaScript会看到“www”。它会发挥一个功能,将用户重定向到输入的链接。这是我的代码:

JavaScript的:

function showAlert() {
    var txtCtrl = document.getElementById("textbox1");
    var txtVal = txtCtrl.value;
    var txtValUpper = txtVal.toUpperCase();
    var txtValLower = txtVal.toLowerCase();
    if (txtVal == '') {
        alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
    } else if (txtValUpper == 'start' || txtValLower == 'start') {
        alert('Hello. What would you like me to do?');
    } else if (txtValUpper.indexOf("weather") != -1 || txtValLower.indexOf("weather") != -1) {
        window.location = "https://www.google.com/#q=weather";
    } else if (txtValUpper.indexOf("time") != -1 || txtValLower.indexOf("time") != -1) {
        alert('The current time according to your computer is' + formatTime(new Date()));
    } else if (txtValUpper.indexOf("help") != -1 || txtValLower.indexOf("help") != -1) {
        window.location = "help/index.html";
    } else if (txtValUpper.indexOf("donate") != -1 || txtValLower.indexOf("donate") != -1) {
        window.location = "donate/index.html";
    } else {
        alert('Sorry, I do not reconise that command. For a list of commands, type "Help" into the text box.');
    }
}
//Show time in 24hour format
function showTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    return [h, m].join(':')
}
//Show time in 12hour format
var formatTime = (function () {
    function addZero(num) {
        return (num >= 0 && num < 10) ? "0" + num : num + "";
    }
    return function (dt) {
        var formatted = '';
        if (dt) {
            var hours24 = dt.getHours();
            var hours = ((hours24 + 11) % 12) + 1;
            formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "PM" : "AM"].join(" ");
        }
        return formatted;
    }
})();

和HTML:

<!doctype html>
<html>
<head>
<title>Random Project</title>
</head>
<body>
<div class="container">
    <img class="logo" src="logo.png" width="450" height="110" alt="Random Project">
    <input type="text" name="textbox1" value="" spellcheck="false" dir="ltr" placeholder="Type here" id="textbox1"><br>
    <button id="button1" name="button1" aria-label="Help me" onClick="showAlert();">
        <span id="button1_text">Help me</span>
    </button>
    <div class="separator"></div>
    <span class="information">&copy; Copyright DemDevs 2013. All rights reserved. Made by Omar Latreche<br><a href="donate/index.html">Donate now</a></span>
    <div class="tip">
        <span class="tip">Tip: </span><span class="tip_text">The commands are NOT case sensitive</span>
    </div>
    <div class="example">
        <span class="example">Example: </span><span class="tip_text">Show me the weather or What is the current time</span>
    </div>
</div>
<div class=""></div>
</body>
</html>

非常感谢任何帮助。

谢谢,奥马尔!

1 个答案:

答案 0 :(得分:1)

使用模式匹配:

var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var t = 'www.google.com'; // you would change this to dynamically check text input

if(t.match(regex))
{
    // Execute your code
}