jQuery validate plugin : accept letters only?

后端 未结 8 1378
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 07:23

I\'m using the validate plugin from http://bassistance.de/jquery-plugins/jquery-plugin-validation/

What i\'m trying to find is a way to make some of my form fields a

8条回答
  •  北海茫月
    2020-11-29 08:01

    The following method to add a custom validator works fine, But the validation happens on keyup, So the error keeps on popping up while the user is typing the text.

    return value.match(new RegExp(""));
    

    The below method works fine for Alphabets and Spaces, Ideal for name fields.

    jQuery.validator.addMethod("alphabetsAndSpacesOnly", function (value, element) {
        return this.optional(element) || /^[a-zA-Z\s]+$/.test(value); });
    
        $("FieldId").rules("add", {
            alphabetsAndSpacesOnly: true,
            messages: { alphabetsAndSpacesOnly: "Please enter a valid name." }
        });
    

提交回复
热议问题