javascript - 如何将我的验证函数变成 ajax 方法?

标签 javascript jquery html css ajax

document.getElementById("button1").addEventListener("click", mouseOver1);

function mouseOver1(){

   document.getElementById("button1").style.color = "red";

}


document.getElementById("button2").addEventListener("click", mouseOver); 


    function mouseOver(){ 

      document.getElementById("button2").style.color = "purple";
   }



$("#button1").hover(function() {
    $(this).css('cursor','pointer');
}); 

$("#button2").hover(function() {
    $(this).css('cursor','pointer');
});

$(document).ready(function() {

   $(".login").validate({
      rules: {
       existingUsername: {
       required: true, 
       minlength: 10
     },
       existingPassword:{
       required: true, 
       minlength: 10 
      },
        
       messages: {
          existingUsername: {
           required: "You must enter a valid existing user account", 
           minlength: "Your username must be at least 10 characters long"
          }, 
           existingPassword: {
           required: "You must enter a valid password associated with an account", 
           minlength: "Your username must be at least 10 characters long"
         }
intro h1 {
  font-family: 'Cambria';
  font-size: 16pt;
  font: bold;
  text-align: left;
}

.intro p {
  font-family: 'Calibri';
  font: italic;
  font-size: 12pt;
  padding: 0px 690px 0px 20px;
  text-align: left;
}

.content {
  border: 2px solid;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
}

#para1 {
  padding: 0px 1050px 0px 20px;
}

#para2 {
  padding: 0px 1099px 0px 20px;
}

.username-label,
.password-label {
 margin: 10px 0px 0px 300px;
 position: relative; 
 top: -70px; 
}

.existing1-username-input-textbox, 
.existing2-password-input-textbox
{
    top: -70px; 
     position: relative; 
      
}

#button1{ 
     background-color: #add8e6;
     margin-left: 390px; 
     position: relative; 
     top: -50px; 
    -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius:10px;
  padding: 0px 20px 0px 20px; 
}

#button2{
  background-color: #add8e6;
  margin-left: -200px; 
  position: relative; 
  top: -30px; 
 -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
  padding: 0px 20px 0px 20px; 

}


.Username-label1, 
.Password-label2,           
.Email-label3, 
.Repeat-Email-label4 
{
  margin: 0px 0px 0px 300px;
  position: relative; 
  top: -70px; 
}
.username-new-input-textbox, 
.password-new-input-textbox, 
.email-new-input-textbox, 
.reenter-new-input-textbox{ 
  position: relative;
   top: -70px;
  margin-left: 20px; 

} 

span{
  
 color: red; 
}
<html>

<head>

  <link href="Home.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <title>Project</title>

</head>

<body>


<div class="container">
  <div class="intro">

    <h1>Welcome to Cuyahoga Community College Student Services Online</h1>

    <p>Cuyahoga Community College recognizes students' rights to access personal and academic records in accordance with the Family Educational Rights and Privacy Act of 1974 (FERPA) as amended by Public Law 93-568.</p>
  </div>
  <br/>

  <div class="content">
    <div class="row top">
      <p id="para1">Already have an account with us? Returning users may log in by entering their site username and password. </p>
      <div class="login">
        <label class="username-label" for="existingUsername">Username</label> 
         <input class="existingUsername" type="text" value=""> 
         <span id="username_error1">*</span><br>

        <label class="password-label" for="existingPassword">Password</label>
        <input class="existingPassword" type="password" value="">
        <span id="password_error2">*</span><br>
        <button id="button1">Log in</button>
      </div>
    </div>
    <hr/>
    <div class="row bottom">
      <p id="para2">New users, please create a new account by providing us with some basic information.</p>

      <div class= "new_customers_info">

        <label class="Username-label1" for="newUsername">Username</label>
        <input class="newUsername" type="text" value="">
         <br/><br/>

        <label class="Password-label2" for="newPassword">Password</label>
        <input class="newPassword" type="password" value="">
         <br/><br/>

        <label class="Email-label3" for="newEmail">Email Address</label>
         <input class="newEmail" type="email" value="" >
          <br/><br/>

        <label class="Repeat-Email-label4" for="repeatEmail">Repeat Email Address</label>
         <input class="repeatEmail" type="email" value="">

        <button id="button2">Create Account</button>
      </div>
    </div>
  </div>
   <br/>
  <footer>Cuyahoga Community College</footer>
  <footer>700 Carnegie Avenue, Cleveland, Ohio, 44115</footer>
</div>
    <script src="Home.js"></script>
</body>
</html>

我如何使用 AJAX 验证每个(上方的文本框)以查看用户名和密码是否正确,如果不正确,则在每个文本框下方以红色显示错误消息。这是我的代码。任何建议都会有所帮助。我知道我可以使用 $.ajax() 方法,但我不知道如何将当前的验证函数转换为合理的 ajax 方法。

最佳答案

$(".selector").validate({
   rules: yourRules,
   messages: {
     username: {
       remote: "Username already exists"
     }
   }
})

// This is cited from Stack Overflow. Would this how Ajax work internally?  

*/http://stackoverflow.com/questions/6772154/using-the-jquery- validate-plugin-with-the-remote-option?rq=1 /*

关于javascript - 如何将我的验证函数变成 ajax 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822204/

相关文章:

jquery - 复选框 css/jquery 样式更改

javascript - Chrome 开发人员工具 - 在 Javascript 中查找匹配的 Name 或 Id 属性?

html - Bootstrap 3 div布局

javascript - 使用 React 发出和处理全局事件

javascript - 如何在 JQuery 中添加和删除类以切换 body 标记上的滚动条

javascript - 使用javascript更改伪后元素的样式

javascript - 使用 Javascript 传递选定的复选框值

javascript - 如何使用 jquery 动画更改背景颜色,例如过渡

php - 如何更改 HTTP GET 请求的最大长度以存储参数?是否可以?

html - 访问过滤结果 Angular 6