---
title: "表单验证"
author: "digilover"
date: "2012-05-04T09:22:50Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2012-04-28T14:10:51Z"
id: 1503373
url: "https://digilover.iteye.com/blog/1503373"
---

&lt;script type="text/javascript" src="system/common/Util.js"&gt;&lt;/script&gt;

&lt;script language="JavaScript"&gt;

function \_check(){

var valid = true;

var username = document.getElementById("username");

var password = document.getElementById("password");

var email = document.getElementById("email");

 

if(username.value.Trim() == ""){

window.alert("用户名不能为空!");

username.value="";

username.focus();

valid = false;

return valid;

}

if(CheckIllegalCharacter(username.value.Trim())){

alert("用户名含有非法字符");

valid = false;

return valid;

}

 

if(password.value.Trim().length&lt;4){

window.alert("密码不能为空且长度不能小于4个字符!");

password.value="";

password.focus();

valid = false;

return valid;

}else{

var idx = email.value.Trim().indexOf('@');

if(idx&lt;=0 || idx==email.value.Trim().length-1){

alert("Email地址格式不合法!");

email.focus();

valid = false;

return valid;

}

}                                                                                                             //window.alert(username.value.Trim().TagTrim().length);  

//window.alert(password.value.Trim().length);  

//window.alert(email.value.Trim().length);  

if(valid){

   document.myform1.submit();

}

}

&lt;/script&gt;

 

\-------------------------------------------------------------------

&lt;form action="[http://www.baidu.com](http://www.baidu.com)" name="myform1" onsubmit="return \_check()"&gt;  
   username:&lt;input type="text" id="username" name="username"&gt;&lt;br&gt;  
   password:&lt;input type="password" id="password" name="password"&gt;&lt;br&gt;  
   email:&lt;input type="text" id="email" name="email"&gt;&lt;br&gt;  
   &lt;input type="submit" name="tijiao"&gt;&lt;br&gt;  
&lt;/form&gt;

&lt;!--EndFragment--&gt;