简体   繁体   English

使用JS使用regex验证用户输入时遇到问题

[英]Having trouble validating user input with regex with JS

I'm a complete noob at this stuff. 我是这个东西的完全菜鸟。 But my goal here is to accept a certain username and validate it with a certain password. 但我的目标是接受某个用户名并使用某个密码验证它。 I've searched and searched all night to no avail so here I am looking for an answer. 我整夜搜索和搜索都没有用,所以我在这里寻找答案。

My code in html: 我在html中的代码:

<form action="" method="POST" id="loginForm">
  <label for="userName" accesskey="F"><br>Username:</label>
  <input type="username" name="userName" id="userName">
  <label for="password"><br>Password:&nbsp;</label>
  <input type="password" name="password" id="password">
  <button>Submit</button>
</form>

My code in a seperate javascript file (which is being called by the html file): 我的代码在一个单独的javascript文件中(由html文件调用):

function validate(id) {
  var guest1User = /guest1/;
  var guest2User= /[guest1]/;
  var guest1Pass= /[guest2]/;
  var guest2Pass= /[guest1]/

  var userCheck= document.getElemetnById(userName);
  var passCheck= document.getElementById(password);
  if (guest1User.test(userCheck.value)) {
    if (guest1Pass.test(passCheck.value)) {
      return true;

    } else { 
      return false; 
    }
  }
  if (guest2User.test(guestCheck.value)) {
    if (guest2Pass.test(passCheck.value)) {
        return true;
    } else {
      return false; 
    }
  } else {
    return false;
  }
}
validate();

Try the following approach: 尝试以下方法:

As per your comment you were wondering how would the js file know that its function has been called.... therefore you need to include the js file in the html file in the following way: 根据您的评论,您想知道js文件将如何知道其函数已被调用....因此您需要以下列方式将js文件包含在html文件中:

<script type="text/javascript" src="somefile.js"></script>

make sure the path to the somefile.js is correct for example if your file system is like that: 确保somefile.js的路径是正确的,例如,如果您的文件系统是这样的:

/root/js/ /根/ JS /

/root/html/ /根/ HTML /

<script type="text/javascript" src="../js/somefile.js"></script>

and your html file is inside the html folder then you refer to the js file as: 并且您的html文件位于html文件夹中,然后您将js文件称为:

 function validate(id) { var guest1User = /guest1/g; var guest2User= /[guest1]/g; var guest1Pass= /[guest2]/g; var guest2Pass= /[guest1]/g; var userCheck= document.getElementById('userName'); var passCheck=document.getElementById('password'); if (guest1User.test(userCheck.value)) { console.log("true"); if (guest1Pass.test(passCheck.value)) { console.log("true"); return true; } else { console.log("false"); return false; } } else { console.log("false"); return false; } } 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <!-- <script type="text/javascript" src="../js/somefile.js"></script> --> </head> <body> <form action="#" method="POST" id="loginForm"> <label for="userName" accesskey="F"><br>Username:</label> <input type="username" name="userName" id="userName"> <label for="password"><br>Password:&nbsp;</label> <input type="password" name="password" id="password"> <input type=submit onclick="return validate();" value="submit"/> </form> </body> </html> 

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

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