Joomla-开源天空

首页 专栏热点 Joomla! 源代码分析 Joomla! 中表单数据验证


Joomla! 中表单数据验证

E-mail

在Joomla! 1.5中可以JHTMLBehavior::formvalidation()这一函数进行表单数据验证,这一函数,引入基于mootools的 validate.js ,文件在 media/system/js 目录下。

在引入了validate.js后,只要设置form 的class  就可以对表单进行验证,下面是实例代码:

 

  1. <?php JHTMLBehavior::formvalidation(); ?>
  2. <script language="javascript">
  3. function myValidate(f) {
  4. if (document.formvalidator.isValid(f)) {
  5. f.check.value='<?=JUtility::getToken()?>';//send token
  6. return true;
  7. }
  8. else {
  9. alert('Some values are not acceptable. Please retry.');
  10. }
  11. return false;
  12. }
  13. </script>
  14. <form id="WV-form" method="post" class="form-validate" onSubmit="return myValidate(this);">
  15. <input type="hidden" name="check" value="post"/>
  16. ...
  17. <input type="text" name="email" size="30" class="required validate-email"/>
  18. ...
  19. <input type="submit" value="Submit" />
  20. </form>

这段代码只是对email输入字段进行验证,我们需要设置 form的class 为 form-validate,同时设置email输入class为 required validate-email ,如果某一输入不需要验证,可以设置class为 invalid

 

  1. defined( '_JEXEC' ) or die( 'Restricted access' ); //Verify Joomla enabled
  2.  
  3. $jAp=&amp; JFactory::getApplication();
  4. if ($_POST['check']!=JUtility::getToken()) {
  5. //First verify if by a javascript error or other possibilities the form has not been submitted without the validation
  6. if ($_POST['check']=='post') $jAp->enqueueMessage('Please check all the fields of the form, aub.<br/>
  7. If your browser blocks javascript, then this form will never be succesfull. This is a security measure.','error');
  8. //If then still the check isn't a valid token, do nothing as this might be a spoof attack or other invalid form submission
  9. return false;
  10. }
  11.  

 

以上代码是服务器接收数据,进行几种验证。

 

Custom handlers

If you want a custome handler, you can add one to the class, as follow example (defined AFTER the validate.js):
  1. Window.onDomReady<span class="br0">(</span><span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  2. document.formvalidator.setHandler<span class="br0">(</span><span class="st0">'birth'</span>, <span class="kw2">function</span><span class="br0">(</span>value<span class="br0">)</span> <span class="br0">{</span>
  3. regex=/^\d<span class="br0">{</span><span class="nu0">4</span><span class="br0">}</span><span class="br0">(</span>-\d<span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span><span class="br0">)</span><span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span>$/;
  4. <span class="kw1">return</span> regex.test<span class="br0">(</span>value<span class="br0">)</span>;
  5. <span class="br0">}</span><span class="br0">)</span>
  6. <span class="br0">}</span><span class="br0">)</span>

You can then set any class to validate-birth to make it validate as yyyy-mm-dd. (Valid date is not checked!)

 

 

相关文章
Joomla! 1.5 API 中文手册(作者:aivera)
Joomla!扩展制作实例教程-模板展示组件-前台路径设置
Joomla!扩展制作实例教程-模板展示组件-前台最终页面
Joomla!扩展制作实例教程-模板展示组件-前台列表页面
Joomla!扩展制作实例教程-模板展示组件-增加后台上传图片功能
Joomla!扩展制作实例教程-模板展示组件-如何数据表增加一个字段
Joomla!扩展制作实例教程-模板展示组件-整理后台列表页面,增加列表分页功能
Joomla!扩展制作实例教程-模板展示组件-后台增加所见即所得编辑器
Joomla!扩展制作实例教程-模板展示组件-后台记录增加和修改程序
Joomla!扩展制作实例教程-模板展示组件-创建组件框架
 

发表您的文章评论

您的姓名 (昵称)
标题:
评分: 很差一般较好很好
评论:
验证码:
请输入验证码

» » 登录 »   -   -