没有编码,但是为朋友做了一个项目。基本上,我需要一个输入表单来检查4位数代码。如果代码是正确的(是2或3个可能的正确代码之一),它将进入新页面,如果不是它没有提交,则显示错误。它不一定非常漂亮。寻找一个jquery解决方案,任何正确方向的推动将不胜感激。我生气了。
<form name="codeForm" action="" method="get">
<input name="yourCode" type="text" id="yourCode"/>
<input name="submit" type="submit" value="Go" id="code_input_submit" />
<div id="message"><p></p>
</div><!--end message-->
</form>
<div id="btn"></div>
和我的jquery一致如下:
$(document).ready(function () {
var code1 = "7816";
var code2 = "1226";
$('#btn').click(function() {
var value = $('#yourCode').val();
if (value == code1){ // go to site 'abc.com/'code1;
}
else if (value == code2){ //go to site 'abc.com/'code2;
}
else{ $('#message').html('your code is wrong');
}
});
});
答案 0 :(得分:0)
如果这是它的接缝,验证要求,你实际上正在考虑存储数据客户端:不,不,不!
每个尝试登录的用户都可以看到您的“入门信息”。
您应该始终对服务器端进行身份验证。
1. store the keys on server (database or file, whatever suits you)
2. client makes a request to server via post
3. Server accepts or denies client request
例如,将表单提交到服务器端页面authenticate.php
PHP文件将检查用户是否合适。
<?php
// very simple and not really secure server-side auth script
$codes = {1234, 5678, 9012};
$valid_user = false; // all users are invalid until otherwise
$user_inserted_code = (int) $_POST['key'];
if(in_array($user_inserted_code, $codes)) {
$valid_user = true;
}
if($valid_user==true)
header("Location: http://example.com/authenticated");
else
header("Location: http://example.com/failled_auth");
?>
如果你真的不需要安全性,而你所提到的代码是静态的,而且(例如)部门代码中的代码......在你的例子中增长,你可以使用一个简单的开关: 强>
$(document).ready(function () {
var code1 = "7816";
var code2 = "1226";
$('#btn').click(function() {
var value = $('#yourCode').val();
switch(value) {
case 7816: window.location = "http://google.com/"; break;
case 1226: window.location = "http://yahoo.com"; break;
default: $('#message').html('your code is wrong');
}
});
});