Home > Web Front-end > JS Tutorial > body text

Ajax validation form example with prompts

亚连
Release: 2018-05-25 10:00:15
Original
1396 people have browsed it

This article mainly introduces the Ajax verification form with prompts, which can realize the function of real-time prompts for form input. It is very simple and practical. Friends in need can refer to it

The example of this article tells the Ajax prompts verification form. Share it with everyone for your reference. The details are as follows:

This is a commonly used Ajax form verification program. It prompts you in real time whether the characters you enter meet the requirements. It is concise and easy to modify. It is implemented in JavaScript and is not mixed with other framework codes. , so it is more practical.

The screenshot of the running effect is as follows:

The specific code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>带提示的Ajax验证表单</title>
<style type="text/css">
form {
  width:500px;
  border:1px solid #ccc;
}
fieldset {
  border:0;
  padding:10px;
  margin:10px;
  position:relative;
}
label {
  display:block;
  font:normal 12px/17px verdana;
}
input {width:160px;}
span.hint {
  font:normal 11px/14px verdana;
  background:#eee url(images/bg-span-hint-gray.gif) no-repeat top left;
  color:#444;
  border:1px solid #888;
  padding:5px 5px 5px 40px;
  width:250px;
  position:absolute;
  margin: -12px 0 0 14px;
  display:none;
}
fieldset.welldone span.hint {
  background:#9fd680 url(images/bg-span-hint-welldone.jpg) no-repeat top left;
  border-color:#749e5c;
  color:#000;
}
fieldset.kindagood span.hint {
  background:#ffffcc url(images/bg-span-hint-kindagood.jpg) no-repeat top left;
  border-color:#cc9933;
}
fieldset.welldone {
  background:transparent url(images/bg-fieldset-welldone.gif) no-repeat 194px 19px;
}
fieldset.kindagood {
  background:transparent url(images/bg-fieldset-kindagood.gif) no-repeat 194px 19px;
}
</style>
<script type="text/javascript">
function checkUsernameForLength(whatYouTyped) {
  var fieldset = whatYouTyped.parentNode;
  var txt = whatYouTyped.value;
  if (txt.length > 5) {
    fieldset.className = "welldone";
  }
  else {
    fieldset.className = "";
  }
}
function checkPassword(whatYouTyped) {
  var fieldset = whatYouTyped.parentNode;
  var txt = whatYouTyped.value;
  if (txt.length > 3 && txt.length < 8) {
    fieldset.className = "kindagood";
  } else if (txt.length > 7) {
    fieldset.className = "welldone";
  } else {
    fieldset.className = "";
  }
}
function checkEmail(whatYouTyped) {
  var fieldset = whatYouTyped.parentNode;
  var txt = whatYouTyped.value;
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
    fieldset.className = "welldone";
  } else {
    fieldset.className = "";
  }
}
function addLoadEvent(func) {
 var oldonload = window.onload;
 if (typeof window.onload != &#39;function&#39;) {
  window.onload = func;
 } else {
  window.onload = function() {
   oldonload();
   func();
  }
 }
}
function prepareInputsForHints() {
 var inputs = document.getElementsByTagName("input");
 for (var i=0; i<inputs.length; i++){
  inputs[i].onfocus = function () {
   this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
  }
  inputs[i].onblur = function () {
   this.parentNode.getElementsByTagName("span")[0].style.display = "none";
  }
 }
}
addLoadEvent(prepareInputsForHints);
</script>
</head>
<body>
<form
  action="#"
  name="basicform"
  id="basicform" >
<fieldset>
  <label for="username">用户名:</label>
  <input
    type="text"
    id="username"
    onkeyup="checkUsernameForLength(this);" />
  <span class="hint">用户名最低6位哦</span>
</fieldset>
<fieldset>
  <label for="password">密码:</label>
  <input
    type="password"
    id="password"
    onkeyup="checkPassword(this);" />
  <span class="hint">密码需要4到8位哦</span>
</fieldset>
<fieldset>
  <label for="email">Email地址:</label>
  <input
    type="text"
    id="email"
    onkeyup="checkEmail(this);" />
  <span class="hint">You can enter your real address without worry - we don&#39;t spam!</span>
</fieldset>
</form>
</body>
</html>
Copy after login

The above is what I compiled for everyone. I hope it will be useful to everyone in the future. helpful.

Related articles:

Implement drop-down box linkage display data based on Ajax

Detailed explanation of ajax synchronization and asynchronousness in jquery

ajax asynchronous upload in jquery

The above is the detailed content of Ajax validation form example with prompts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template