使用JavaScript验证文本文件

时间:2017-01-16 14:24:47

标签: javascript php verification

我正在寻找使用文本文件进行简单验证。

例如:

verification.txt

Text: ABC5864

根据用户输入,如果正确,则允许登录,否则显示错误消息:

<input name="myverification" id="myverification" type="verification" class="form-control" placeholder="verification">

这是我的代码:

$(document).ready(function () {
    "use strict";
    $("#submit").click(function () {

        var username = $("#myusername").val(), password = $("#mypassword").val();

        if ((username === "") || (password === "")) {
            $("#message").html("<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Please enter a username and a password</div>");
        } else {
            $.ajax({
                type: "POST",
                url: "checklogin.php",
                data: "myusername=" + username + "&mypassword=" + password,
                dataType: 'JSON',
                success: function (html) {
                    //console.log(html.response + ' ' + html.username);
                    if (html.response === 'true') {
                        //location.assign("../index.php");
                       location.reload();
                        return html.username;
                    } else {
                        $("#message").html(html.response);
                    }
                },
                error: function (textStatus, errorThrown) {
                    console.log(textStatus);
                    console.log(errorThrown);
                },
                beforeSend: function () {
                    $("#message").html("<p class='text-center'><img src='images/ajax-loader.gif'></p>");
                }
            });
        }
        return false;
    });
});
      <form class="form-signin" name="form1" method="post" action="checklogin.php">
        <h2 class="form-signin-heading">Please sign in</h2>
        <input name="myusername" id="myusername" type="text" class="form-control" placeholder="Username" autofocus>
        <input name="myverification" id="myverification" type="verification" class="form-control" placeholder="verification">
        <input name="mypassword" id="mypassword" type="password" class="form-control" placeholder="Password">
        <button name="Submit" id="submit" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        <div id="message"></div>
      </form>

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个

  function checkInput(input_id) {
        input_value = jQuery('#' + input_id).val();
        if ((input_value == "") || (input_value == null)) {
            jQuery('#' + input_id + '_error').html('Please fill in
!');
            return 0;
        } else {
            jQuery('#' + input_id + '_error').html('');
            return 1;
        }

    }