shole shole - 6 months ago 58
jQuery Question

Kendo validator seems ignore pattern attribute (regex)



<input id="myID"
name="myName"
class="form-control k-invalid"
data-bind="value: ViewModel.JobNo, visible: Layout.isEditMode"
required="required"
data-required-msg="The Job No. field is required."
pattern="\d{10}"
>





I have an input element as above in a form, which I want to force user to enter 10 digit numbers.

I simply add a
pattern = "\d{10}"
and thought when I called
validator.validate()
, it will handle the validation for me, but seems it's not the case, indeed the
pattern
rule is always ignored while the
required
rule works perfectly.

I tried to verify it, so I opend developer console in Chrome, and typed the following:



$("#myID").kendoValidator();
$("#myID").data("kendoValidator").validate()





If I leave the field empty, this will give me false, and say it is required (good).

But if I have typed in some numbers, or any other strings which should violate the pattern, this still gives me true and gets pass the validation.

What did I do wrong, and how can I achieve what I wanted? Thanks!

Answer

That is probably because you are missing the type attribute from input tag. Adding a type attribute should fix the issue.

<input id="myID" 
       type="text" <!-- enter what ever type of input it is-->
       name="myName" 
       class="form-control k-invalid" 
       data-bind="value: ViewModel.JobNo, visible: Layout.isEditMode"
       required="required" 
       data-required-msg="The Job No. field is required." 
       pattern="\d{10}" 
>

I made a telerik dojo using an existing example just to show the working. The first input has no type and hence pattern validation doesn't work, but it works on second input.

Comments