[英]How do you make a textbox with required words js+HTML
今天我想问一个关于形式(kinda)的问题。 好的,所以我想要在用户将文本放在文本区域的地方,但是只有在输入某些字符串时,它才会转到下一页(类似于codeacademy的编辑器)
<HTML>
<HEAD>
<TITLE>this is NOT a password screen!</TITLE>
<SCRIPT language="JavaScript">
<!--hide
var password=prompt('Enter the text:','');
var mypassword="word";
if (password==myword)
{
window.location="pass.html";
}
else
{
window.location="nopass.htm";
}
var myword2="a password";
if (password==myword2)
{
window.location="core2.html";
}
else
{
window.location="nopass.htm";
}
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
埃里克(Eric)给出了更好,更复杂的答案,我同意您不应该对密码使用此方法,但是如果您想要一种与您尝试执行的操作更相称的方法,请根据使用的内容使用indexOf函数或Substring函数你想做。
var userInput = //however you choose to get user input
if (userInput.indexOf("pass1") > -1) {//user input has pass1 in it somewhere
window.location="pass.html";
}
else if (userInput.indexOf("pass2") > -1) //user input has pass2 in it somewhere {
window.location="pass2.html";
}
else {
window.location="nopass.html";
}
如果请求的字符串在原始字符串中不存在,则indexOf返回-1;如果请求的字符串不存在,则返回一个从0开始的数字,其中0是第一个字符。 如果您只想查看userInput中的特定区域是否是特定单词,则将If语句中的所有内容替换为
(userInput.subString(0, 5) == expectedInput)
请注意,0是第一个字符,5是第六个字符,但是子字符串在第二个输入处停止并且不添加它,因此将不返回第六个字符,仅返回第一个5。
因此,如果userInput为“ thepass1”,则第一部分将简化为“ thePa”,而如果ExpectedInput为“ pass1”,则它将失败。 但是,如果userInput为“ pass1 is the Password”,则它将通过,因为它将简化为“ pass1”。
在indexOf示例中,因为pass1位于字符串中的某个位置,所以两者都将通过第一次检查。
我假设这是一个人为设计的示例,因为您永远不要野蛮地检查客户端计算机上的密码。
密码检查应始终在服务器端进行,否则,您只是将钥匙放在门垫下。
就是说,这里是一个示例,您可以根据用户键入的内容导航到其他页面 。 您的示例使我相信,每个“密码”都应将您发送到不同的页面。
https://jsfiddle.net/Ldar81s6/
//array of objects that store 'password' and destination
var passwordLinks = [{
password: "pass1",
page: "myPage1.html"
}, {
password: "pass2",
page: "myPage2.html"
}, {
password: "pass3",
page: "myPage3.html"
}, {
password: "pass4",
page: "myPage4.html"
}];
//define our check password function
function CheckPwd() {
var password = prompt('Enter the password:', '');
//clicking cancel gives null
if (password == null) {
alert("action cancelled");
return;
}
for (var i = 0; i < passwordLinks.length; i++) {
//object in the array at current position of our for loop
passObj = passwordLinks[i];
//if we matched, go to the page (I just did alert)
//also return because we don't need to check anymore, we have what we need
//check against passObj's password
if (passObj.password == password) {
alert(passObj.page);
return;
}
}
//if we haven't exited the function yet, it means that we never matched the password
alert("incorrect password");
}
//call our check password function
CheckPwd();
让我知道什么有意义/什么没有
好的,对不起,我没有正确说出这句话,我想要一种可以像这样的单词:
食物鸡框 ,如果在文本框中的任何实例中输入了一个短语,您都可以通过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.