Prevent numerical values in kendo textbox

3.6k Views Asked by At

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;
            }});
2

There are 2 best solutions below

0
On

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

4
On

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.