Wu Yuxiong - natural born JAVASCRIPT development of learning: Form Validation

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</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"</>= "submit"value
form>
    
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</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 ( " is not a valid e-mail address " );
           return  to 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>

 

Guess you like

Origin www.cnblogs.com/tszr/p/10942866.html