2

I am creating a sample book library application using asp.net MVC in which i am using kendo UI tools, i want use Book title or author fields in which i want to prevent a user from entering numerical values, allowing only A-Z, how will i use kendoValidator to do that. below is my input

<input id="txtTitle" name="txtTitle" type="text" class="k-textbox" value="#= Title #" />

This is where i want to do the validation

return $("#bookDiv").kendoValidator({
        rules: {
            Title: function (input) {
                if (input.is("[name=txtTitle]") && input.val() == ""))
                    return false;
                else
                    return true;
            }});
Zubair Afridi
  • 31
  • 1
  • 3

2 Answers2

0

By using a regex and match:

(input.is("[name=txtTitle]") && input.val() == "" && input.val().match(/^[a-zA-Z]+$/)))

/^[a-zA-Z]+$/ means

  • search from the beginning of the string until the end. (^ till $)
  • Search for a-z and A-Z
  • + for all characters until the end of the string.

For a match the string needs to be a-z entirely. If there is a match it will not return null and pass.

On a side note: This will not allow any other character. Diacritics like é will not pass.

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • Still not working for me, do i have to add some kendovalidator specific attribute to my input or not needed – Zubair Afridi Aug 10 '15 at 10:18
  • Still you need to call the validator on submit/blur of the input/form, setting it on a `div` will not let it auto validate. Setting it on a form auto validates when the user submits. – Mouser Aug 10 '15 at 10:23
  • yep i am already doing that " var validator = applyValidator(); and then if (validator.validate()) { my code here after validation}); " – Zubair Afridi Aug 10 '15 at 10:28
  • Please update your post with the full code for validation'. Your problem isn't reproduceable at the moment. – Mouser Aug 10 '15 at 10:31
0

Rather than creating a custom rule, you could use Kendo UI's built in pattern validator.

<input id="title" name="title" type="text" value="#=Title#" pattern="[^0-9]+" />

Kendo UI API Reference

Brett
  • 4,268
  • 1
  • 13
  • 28