dijit ValidationTextBox如何添加minlength和maxlength

时间:2015-09-01 08:20:04

标签: javascript dojo dijit.form

我想知道如何(使用简单的工作示例)将maxlengthminlength添加到由

生成的输入标记中
dijit/form/ValidationTextBox

所需输出的示例:

<input maxlength="10" minlength="2" class="dijitReset dijitInputInner" data-dojo-attach-point="textbox,focusNode" autocomplete="off" name="title" type="text" tabindex="0" id="Image-1-title-LabelValidationTextBox" aria-required="undefined" value="Title text for image Image-1" aria-invalid="false">

3 个答案:

答案 0 :(得分:2)

使用正则表达式约束

尝试此示例
<input type="text" value="someTestString" required="true"
data-dojo-type="dijit/form/ValidationTextBox"
data-dojo-props="regExp: '[a-zA-Z0-9$%!_]{2,10}', invalidMessage: 'The value must be at least 2 character and maximum 10'" />

答案 1 :(得分:2)

ValidationTextBox具有minLength和maxLength属性。它们以声明的方式用于以下原因。

  <input  data-dojo-type="dijit/form/ValidationTextBox"
            data-dojo-props="required:true,
                            maxLength:5,
                            minLength:2"
                            type="text" 
                            name="exmaple" />

答案 2 :(得分:0)

这是另一种解决方案:

registry.byId("validationTextBox").validator = function(value, constraints) {
    if(value) {
        return value.length >= 2 && value.length <= 10;
    }
    return true;
}