简体   繁体   English

验证并允许特定的用户名登录HTML的用户名文本字段

[英]Validate and allow specific username to login in username text field of HTML

I want to allow only two selected users to login to my application on-click of submit button . 我只允许两个选定的用户单击“提交”按钮登录到我的应用程序。 These two usernames are listed in a text file called as user.txt so, the username parameter should be read and if the username entered in the text field matches the entries of the user.txt file then the user must be allowed to login else error should be displayed. 这两个用户名列在名为user.txt的文本文件中,因此,应读取username参数,如果在文本字段中输入的用户名与user.txt文件的条目匹配,则必须允许该用户登录,否则出错应该显示。

Note: The challenge here I already have one validation( validateForm()) which is working fine on click of the submit button. 注意:这里的挑战是我已经有一个validation( validateForm()) ,单击Submit按钮可以正常工作。 It checks to ensure the username and password fields are not empty. 它检查以确保用户名和密码字段不为空。 I need this new specific username allow validation validation as well on-click of submit button. 我需要这个新的特定用户名,以允许进行验证验证以及单击“提交”按钮。

Can someone suggest me example codes how can I handle this validation using Ajax or jQuery or simple JavaScript? 有人可以建议我示例代码,如何使用Ajax或jQuery或简单的JavaScript处理此验证?

Username.txt file Username.txt文件

Username1 : Tom
Username2 : Harry

Check whether username and password text fields are not empty: 检查用户名和密码文本字段是否不为空:

function validateForm() {
    var x = document.forms["login"]["userid"].value;
    if (x == null || x == "") {
        alert("Username is empty");
        return false;
    }
    var x = document.forms["login"]["pswrd"].value;
    if (x == null || x == "") {
        alert("Password is empty");
        return false;
    }
}

You need to check against the server, where the file.txt is located (btw: databases are used for that purpose, usually). 您需要检查file.txt所在的服务器(顺便说一句:通常将数据库用于此目的)。

The only way to do it on client side (javascript), is to have the list of names inside an array - but that doesn't make sense - as the client would be able to see the array. 在客户端(javascript)上执行此操作的唯一方法是将名称列表包含在数组中-但这没有意义-因为客户端可以看到该数组。 So you really should use server side for that purpose. 因此,您确实应该为此目的使用服务器端。

Have a look at tutorials like this one if you want proper (secure) authentication. 如果您要进行正确的(安全)身份验证,请参阅此类教程。

If you only want to check against the usernames, try this jQuery code: 如果只想检查用户名,请尝试以下jQuery代码:

$.ajax({
    type: "POST",
    url: "script_url",
    dataType: "json",
    data: {
        user : $("#user").val(),
        call : "checkUser"  
    },
    beforeSend: function(){ },
    success : function(response){
        if(!response){
            alert('User not allowed!');
        }
    },
    complete: function(){ }
})

And PHP code: 和PHP代码:

$lines = file('../location/Username.txt');
$arr_users = array();
foreach($lines as $line){
    $arr = explode(':', $line);
    $arr_users[] = trim($arr[1]);
}

function checkUser($request){
    return in_array($request['user']);
}

if (!empty($_POST['call'])) { 
    die($_POST['call']($_POST));
}

Best regards! 最好的祝福!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM