Validating Common Form Input - Part 1

简介:

1.准备测试的Form

 
  1. <form onsubmit="return validate(this);"> 
  2.   Username<br /> 
  3.   <input type="text" name="username" value="" /><br /> 
  4.   Password<br /> 
  5.   <input type="password" name="password1" value="" /><br /> 
  6.   Repeat password<br /> 
  7.   <input type="password" name="password1" value="" /><br /> 
  8.   Name<br /> 
  9.   <input type="text" name="name" value="" /><br /> 
  10.   Address<br /> 
  11.   <input type="text" name="address" value="" /><br /> 
  12.   Phone<br /> 
  13.   <input type="text" name="phone" value="" /><br /> 
  14.   Email<br /> 
  15.   <input type="text" name="email" value="" /><br /> 
  16.   Website<br /> 
  17.   <input type="text" name="website" value="" /><br /> 
  18.   <input type="Submit" value="Submit" /> 
  19. </form> 

2.为form 添加验证事件

 
  1. <form onsubmit="return validate(this);"> 
  2. ...  
  3. </form> 

3.一个简单的验证方法(验证单个元素不能为空)

 
  1. function validate(form) {  
  2.   // If no value in Username input field  
  3.   if(!form.elements.username.value)  
  4.     return false;  
  5.   else 
  6.     return true;  
  7. }  

4.第二个验证方法(验证所有元素不能为空)

 
  1. function validate(form) {  
  2.   // For every input field in the form  
  3.   for(var i=0; i<form.elements.length; i++) {  
  4.     // If the input field is empty  
  5.     if(!form.elements[i].value)  
  6.       return false;  
  7.   }  
  8.   return true;  
  9. }  

 



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/456621,如需转载请自行联系原作者

相关文章
|
12月前
poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
314 0
|
JSON 数据格式
Content type ‘multipart/form-data;boundary=------57031299820747271;charset=UTF-8‘ not supported的解决方案
Content type ‘multipart/form-data;boundary=------57031299820747271;charset=UTF-8‘ not supported的解决方案
246 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
(standard input): No keywords in input file
(standard input): No keywords in input file
117 0
|
Java
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
401 0
|
Java
Hybris DDIC type and its counterpart model class
Hybris DDIC type and its counterpart model class
132 0
Hybris DDIC type and its counterpart model class
Cannot find source code based button in SE24
When you are logging on to customer system for incident handling, you want to switch to source code to perform some keyword search. However, you could not find button “Source code based builder” in toolbar, with following warning message: ———————————————— 版权声明:本文为CSDN博主「汪子熙」的原创文章,遵循CC 4.0 BY-SA版权协
127 0
Cannot find source code based button in SE24
How to enable multiple text type for Product
Created by Jerry Wang, last modified on Oct 31, 2014
127 0
How to enable multiple text type for Product
Posted content type isn't multipart/form-data
Posted content type isn't multipart/form-data
513 0