2013-02-21 181 views
0

我是javascript新手。我试图根据他们输入的“客户端ID”创建一个Web重定向到客户端的开发站点。Javascript用户输入验证

这是我到目前为止,你会看到我正在完全倒退。

我想重定向到一个新标签中的URL。但是如果他们输入错误的客户端ID,会弹出一个提示“对不起,再试一次”。随着警报的添加,我无论是否输入正确的“代码”,都会弹出一个窗口。

<head> 
<script> 

function validateCode() 
{ 
var codeTextBox = document.getElementById("codeTextBox").value; 

if (codeTextBox == 'C1') 
{ 
    window.location.href = "http://www.client1.com"; 
} 
if (codeTextBox == 'C2') 
{ 
    window.location.href = "http://www.client2.com"; 
} 
if (codeTextBox == 'C3') 
{ 
    window.location.href = "http://www.client3.com"; 
} 
if (codeTextBox == 'C4') 
{ 
    window.location.href = "http://www.client4.com"; 
} else { 
    alert('Sorry, try again.'); 
    } 

} 

</script> 
</head> 

<body> 
Enter User ID: 
<input type="text" name="codeTextBox" id="codeTextBox" /> 
<input type="submit" name="Submit" value="Submit" onclick="validateCode()" /> 
</body> 

对我来说很简单:)谢谢!

+0

使用'其他if',而不是仅仅'if' – Huy 2013-02-21 15:35:29

+0

使用开关的情况下 – 2013-02-21 15:36:05

+0

随着你看到警报代码“对不起,再试一次简单的多。 “每次你写“C4”以外的东西。 – Nebril 2013-02-21 15:45:10

回答

0
if (codeTextBox == 'C1') 
{ 
    window.location.href = "http://www.client1.com"; 
} 
else if (codeTextBox == 'C2') 
{ 
    window.location.href = "http://www.client2.com"; 
} 
else if (codeTextBox == 'C3') 
{ 
    window.location.href = "http://www.client3.com"; 
} 
else if (codeTextBox == 'C4') 
{ 
    window.location.href = "http://www.client4.com"; 
} 
else { 
    alert('Sorry, try again.'); 
    } 
+0

这工作!但我如何打开新选项卡中的网址? – kaitsie22 2013-02-21 15:44:37

+0

要打开一个新的标签,你可以做window.open('[url]','_ blank')这里指定http://www.w3schools.com/jsref/met_win_open.asp – 2013-02-21 20:40:23

0

试试这个:

<head> 
<script> 

function validateCode(){ 
    var codeTextBox = document.getElementById("codeTextBox").value; 

    if (codeTextBox == 'C1') 
    { 
     window.location.href = "http://www.client1.com"; 
    } 
    else if (codeTextBox == 'C2') 
    { 
     window.location.href = "http://www.client2.com"; 
    } 
    else if (codeTextBox == 'C3') 
    { 
     window.location.href = "http://www.client3.com"; 
    } 
    else if (codeTextBox == 'C4') 
    { 
     window.location.href = "http://www.client4.com"; 
    } 
    else 
    { 
     alert('Sorry, try again.'); 
    } 

} 

</script> 
</head> 

<body> 
    Enter User ID: 
    <input type="text" name="codeTextBox" id="codeTextBox" /> 
    <input type="submit" name="Submit" value="Submit" onclick="validateCode()" /> 
</body> 
0

的事情是,如果公司是独立的所以即使值是“C1”,那么最后的“其他”如果仍然会执行。

你可以这样做:

if (codeTextBox == 'C1') 
{ 
    window.location.href = "http://www.client1.com"; 
} 
else if (codeTextBox == 'C2') 
{ 
    window.location.href = "http://www.client2.com"; 
} 
else if (codeTextBox == 'C3') 
{ 
    window.location.href = "http://www.client3.com"; 
} 
else if (codeTextBox == 'C4') 
{ 
    window.location.href = "http://www.client4.com"; 
} 
else 
{ 
    alert('Sorry, try again.'); 
} 

或开关

switch(codeTextBox) 
{ 
case 'C1': 
    window.location.href = "http://www.client1.com"; 
    break; 
. 
. 
. 

default: 
    alert('Sorry, try again.'); 
} 
+0

大声笑,问题是一半一分钟,我甚至无法及时完成答案:P – qiuntus 2013-02-21 15:39:25

+0

haha​​ha stackoverflow挑战的所有部分 – Huy 2013-02-21 15:42:04

1

去尝试使用 “否则,如果” 对C2/C3/C4的检查。或者,您可以查看如何使用switch语句。 (查看所有其他答案。)

JavaScript并不总是按照您可能期望的顺序执行操作;这里您希望立即重定向到新页面,但它希望先完成代码,因此只要代码不是C4,就会发生警报。

1

使用开关盒,这里是示例如何在您的代码中使用。休息你完成它。

function validateCode(){ 
    var codeTextBox = document.getElementById("codeTextBox").value; 

    switch(codeTextBox){ 
    case 'C1': 
     window.location.href = "http://www.client1.com"; 
     break; 
    case 'C2': 
     window.location.href = "http://www.client2.com"; 
     break; 
    ... 
    ... 
    ... 
    default: 
     alert('Sorry, try again.'); 
    } 
} 
0

这是在我看来:)

var redirectUrls = {c1: 'c1.com', c2: 'c2.com', c3: 'c3.com' }; 

if (codeTextBox in redirectUrls) { 
    window.location.href = redirectUrls[codeTextBox];  
} else { 
    alert('Sorry, try again.'); 
}