首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPages中的bootstrapValidator

XPages中的bootstrapValidator
EN

Stack Overflow用户
提问于 2019-01-14 01:15:54
回答 1查看 163关注 0票数 0

我在我的XPage中有以下代码,但是它对给定的字段ipAddress不起作用,即使它是空的,它也不会提示任何验证,我也尝试了#{id:ipAddress},它不起作用。我使用的是Notes9和Designer9。有没有人可以帮助我使用bootstrapValidator功能在XPages中做字段验证的其他方法。

代码语言:javascript
复制
     <?xml version="1.0" encoding="UTF-8"?>
            <xp:view xmlns:xp="http://www.ibm.com/xsp/core" id="testid">
            <xp:this.resources>
                <xp:styleSheet href="/bootstrap.css"></xp:styleSheet>
                <xp:styleSheet href="/bootstrapValidator.min.css"></xp:styleSheet>
                <xp:script src="/jquery-1.11.1.js" clientSide="true"></xp:script>
                <xp:script src="/bootstrap.min.js" clientSide="true"></xp:script>
                <xp:script src="/bootstrapValidator.js" clientSide="true"></xp:script>
                <xp:script src="/jquery.mask.min.js" clientSide="true"></xp:script>
            </xp:this.resources>
            <xp:form id="maskForm">
          <div class="col-lg-5">
      <xp:inputText id="ipAddress" title="ipAddress">
        </xp:inputText>
        </div>
            </xp:form>
            <xp:scriptBlock>
                <xp:this.value><![CDATA 

var test ="view:_id1";
        var fieldvalue="#view:_id1:_id4:_id142:_id143:callback1:idLocation";
      XSP.addOnLoad( function() {
        $("#"+test).bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
           fieldvalue: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: 'The username is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    }
                }
            } } }) });

            ]]></xp:this.value>
            </xp:scriptBlock>
            </xp:view>

    <!-- end snippet -->

附件图片在这里,输出后,您的建议image of error

imager of errorerro 3

EN

回答 1

Stack Overflow用户

发布于 2019-01-14 09:40:36

当你检查你的HTML源文件时,你会发现id属性已经被修改了。HTML规范要求ID是惟一的,因此JSF修改了名称。

在您的脚本块中,您需要将它们存储在变量中,而不是逐字使用它们。

您已经为表单这样做了。对字段也是如此。

更新

您的代码可能需要如下所示(在CDATA中):

代码语言:javascript
复制
      XSP.addOnLoad( function() {
     // You might need one more #
        $('#{id:maskForm}').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
           '#{id:ipAddress}': {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: 'The username is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    }
                }
            } } }) });

        ]]&gt;

您的代码片段中唯一的字段是ipAddress --这就是我的示例所使用的。这里的诀窍是:用给出实际id的XPages表达式语言替换静态id。脚本块在将数据发送到浏览器之前处理表达式。浏览器在不知道数据经过预处理的情况下获取数据。

因此,如果您有<input id="Color" />,那么您可以使用#{id:Color}来获取实际的id。

但是,您可能会重新考虑您的方法。XPages有很好的验证器,它们存储在一个字段中,并提供了您想要的大部分内容。将验证外部化,当字段发生变化时,您需要不断地更改代码。将字段和验证放在一起!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54171271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档