如何在 drupal 7 中应用 Web 表单验证?

发布于 2024-11-26 02:32:16 字数 112 浏览 6 评论 0原文

我的 drupal 7 网站中有网络表单。我想要的是验证我的网络表单字段。 Web 表单包含一个电话字段,该字段应接受数字字段,并且应仅包含 10 个数字。是否有任何模块可以实现此目的,或者我必须为此编写代码。

I have webforms in my drupal 7 website. What I want is to validate my webform fields. The webform contains a phone field which should accept numeric field and should contain 10 numbers only. Is there any module for this or will I have to code for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-12-03 02:32:16

使用 hook_form_alter() 在 drupal 创建模块中应用自定义验证,

例如 mymodule

文件 mymodule.module

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
    print $form_id;

    if($form_id=='webform_client_form_1') //Change webform id according to you webformid
    {
        $form['#validate'][]='mymodule_form_validate';
        return $form;
    }
}

function mymodule_form_validate($form,&$form_state)
{
    //where "phone" is field name of webform phone field
    $phoneval = $form_state['values']['submitted']['phone'];

    if($phoneval=='')
    {
        form_set_error('phone','Please fill the form field');
    }
    // Then use regular expression to validate it.
    // In above example i have check if phonefield is empty or not.
}

如果您想了解更多如何使用 hook_form_alter() 的详细信息,请访问此链接 http://www.codeinsects.com/drupal-hook-system-part-2.html

Use hook_form_alter() to apply custom validation in drupal

create module e.g. mymodule

file mymodule.module

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
    print $form_id;

    if($form_id=='webform_client_form_1') //Change webform id according to you webformid
    {
        $form['#validate'][]='mymodule_form_validate';
        return $form;
    }
}

function mymodule_form_validate($form,&$form_state)
{
    //where "phone" is field name of webform phone field
    $phoneval = $form_state['values']['submitted']['phone'];

    if($phoneval=='')
    {
        form_set_error('phone','Please fill the form field');
    }
    // Then use regular expression to validate it.
    // In above example i have check if phonefield is empty or not.
}

If you want to more to detail how to use hook_form_alter() visit this link http://www.codeinsects.com/drupal-hook-system-part-2.html

只等公子 2024-12-03 02:32:16

有一个名为 Webform Validation 的模块,我们可以在其中为每个字段设置验证规则。

以下是其项目页面的摘录:

...向每个 Webform 节点添加一个额外的选项卡,允许您为 Webform 组件指定验证规则。您可以创建一个或多个预定义的验证规则,并选择应根据这些规则验证哪些 Web 表单组件。通过使用该模块提供的钩子,您还可以在自己的模块中定义自己的验证规则。

There is a module named Webform Validation where we can set validation rules for each fields.

Here is an excerpt from its project page:

... adds an extra tab to each webform node, allowing you to specify validation rules for your webform components. You can create one or more of the predefined validation rules, and select which webform component(s) should be validated against those. By using the hooks provided by this module, you can also define your own validation rules in your own modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文