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,如需转载请自行联系原作者

相关文章
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]
382 0
|
7月前
|
IDE 开发工具 Android开发
Couldn‘t get post build model. Module:UpdateService_0804.main Variant: debugOpen logcat panel fo
Couldn‘t get post build model. Module:UpdateService_0804.main Variant: debugOpen logcat panel fo
90 0
|
7月前
|
前端开发 JavaScript
Error_ Multipart_ Boundary not foun
Error_ Multipart_ Boundary not foun
117 0
|
Java 应用服务中间件 Linux
SpringBoot - Processing of multipart/form-data request failed. Unexpected EOF read on the socket
SpringBoot - Processing of multipart/form-data request failed. Unexpected EOF read on the socket
1598 0
SpringBoot - Processing of multipart/form-data request failed. Unexpected EOF read on the socket
|
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的解决方案
325 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
|
Java
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
447 0
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版权协
Cannot find source code based button in SE24
Posted content type isn't multipart/form-data
Posted content type isn't multipart/form-data
533 0