JavaScript form validation

JavaScript Form Validation


JavaScript Form Validation

JavaScript available to validate the input data in the HTML form before the data is sent to the server.

Form data often requires the use of JavaScript to verify its correctness:

Verify whether the form data is empty?

Verify whether the input is a correct email address?

Verify whether the date is entered correctly?

Verify whether the form input content is numeric?

Required (or required) items

The following function is used to check whether the user has filled in the required (or required) items in the form. If the required field or required field is empty, the warning box will pop up and the return value of the function is false. Otherwise, the return value of the function is true (meaning there is no problem with the data):

function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert ("Last name must be filled in");
return false;
}
}

The above function is called when the form is submitted:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<head>
<script>
function validateForm(){
var x=document.forms["myForm"]["fname"].value;
if (x==null || x==""){
  alert("姓必须填写");
  return false;
  }
}
</script>
</head>
<body>    
<form name="myForm" action="demo-form.php" onsubmit="return validateForm()" method="post">
姓: <input type="text" name="fname">
<input type="submit" value="提交">
</form>  
</body>
</html>

E-mail Validation

The following function checks whether the entered data conforms to the basic syntax of an email address.

This means that the input data must contain the @ symbol and the period (.). At the same time, @ cannot be the first character of the email address, and there must be at least one period after @:

function validateForm(){
var x=document.forms["myForm"]["email "].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
alert("Not a valid e-mail address");
return false;
}
}

The following is the complete code along with the HTML form:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<head>
<script>
function validateForm(){
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
        alert("不是一个有效的 e-mail 地址");
        return false;
    }
}
</script>
</head>
<body>    
<form name="myForm" action="demo-form.php" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="提交">
</form>    
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>表单验证</title> <script type="text/javascript"> //校验输入 function checkInput(){ var flag=false; var username=$("username").value; var pwd=$("pwd").value; var repwd=$("repwd").value; var email=$("email").value; if(username==""){ alert("用户名不能为空!"); }else if(pwd==""){ alert("密码不能为空!"); }else if(pwd.length<6){ alert("密码必须大于6位"); }else if(pwd!=repwd){ alert("密码不一致!"); }else if(email.indexOf("@")==-1 ||email.indexOf(".")==-1){ alert("邮箱格式不正确!"); }else{ flag=true; } //限定用户名不能包含数字 for(var i=0;i<username.length;i++){ var s=username.substring(i,i+1); if(!isNaN(s)){ alert("用户名不能包含数字!") return false; } } return flag; } //获取焦点时清除原内容 function clearUsername(){ var username=$("username"); username.style.border="1px solid #f00"; if(username.value=="请输入正确的用户名"){ username.value=""; } } //失去焦点时检验用户名 function checkUsername(username){ username.style.border=""; if(username.value==""){ $("usernameinfo").style.font="normal 15px 宋体"; $("usernameinfo").style.color="#f00"; $("usernameinfo").innerHTML="用户名不能为空"; username.focus(); }else if(username.value.length>14||username.value.length<6){ $("usernameinfo").style.font="normal 15px 宋体"; $("usernameinfo").style.color="#F00"; $("usernameinfo").innerHTML="用户名长度必须在6-14之间!"; //username.select();//此代码在IE和Chrome中好使,在Firefox中不好使 //匿名函数 setTimeout(function(){username.select();},0); }else{ $("usernameinfo").innerHTML=""; } } function $(id){ return document.getElementById(id); } </script> </head> <body> <form action="success.html" method="post" onsubmit="return checkInput();"> <table border="0" cellpadding="0" cellspacing="0" width="600px"> <tr> <td align="right">用户名:</td> <td><input type="text" name="userbname" id="username" value="请输入正确的用户名" onfocus="clearUsername()" onblur="checkUsername(this)"/><span id="usernameinfo"></span></td> </tr> <tr> <td align="right">密码:</td> <td><input type="password" name="pwd" id="pwd"/></td> </tr> <tr> <td align="right">确认密码:</td> <td><input type="password" name="repwd" id="repwd"/></td> </tr> <tr> <td align="right">电子邮箱:</td> <td><input type="text" name="email" id="email"/><br/></td> </tr> <tr> <td></td> <td>  <input type="submit" value="注册"/>  <input type="reset" value="重置"/></td> </tr> </table> </form> </body> </html>
submitReset Code