亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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("姓必須填寫(xiě)");
  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("不是一個(gè)有效的 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>表單驗(yàn)證</title> <script type="text/javascript"> //校驗(yàn)輸入 function checkInput(){ var flag=false; var username=$("username").value; var pwd=$("pwd").value; var repwd=$("repwd").value; var email=$("email").value; if(username==""){ alert("用戶(hù)名不能為空!"); }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; } //限定用戶(hù)名不能包含數(shù)字 for(var i=0;i<username.length;i++){ var s=username.substring(i,i+1); if(!isNaN(s)){ alert("用戶(hù)名不能包含數(shù)字!") return false; } } return flag; } //獲取焦點(diǎn)時(shí)清除原內(nèi)容 function clearUsername(){ var username=$("username"); username.style.border="1px solid #f00"; if(username.value=="請(qǐng)輸入正確的用戶(hù)名"){ username.value=""; } } //失去焦點(diǎn)時(shí)檢驗(yàn)用戶(hù)名 function checkUsername(username){ username.style.border=""; if(username.value==""){ $("usernameinfo").style.font="normal 15px 宋體"; $("usernameinfo").style.color="#f00"; $("usernameinfo").innerHTML="用戶(hù)名不能為空"; username.focus(); }else if(username.value.length>14||username.value.length<6){ $("usernameinfo").style.font="normal 15px 宋體"; $("usernameinfo").style.color="#F00"; $("usernameinfo").innerHTML="用戶(hù)名長(zhǎng)度必須在6-14之間!"; //username.select();//此代碼在IE和Chrome中好使,在Firefox中不好使 //匿名函數(shù) 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">用戶(hù)名:</td> <td><input type="text" name="userbname" id="username" value="請(qǐng)輸入正確的用戶(hù)名" 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">確認(rèn)密碼:</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="注冊(cè)"/>  <input type="reset" value="重置"/></td> </tr> </table> </form> </body> </html>
submitReset Code