0

I am using validate() to validate this form. I have used the addMethod() but for some reason it is still accepting alphabetic and numeric characters in the First Name.

This is what I have, what am I doing wrong?

$(document).ready (function (){

    $('#Form').validate({
        rules: {
            firstN:  {
                required: true,
                minlenght: 1,
                alpha: true

            },
            email: {
                required: true,
                email: true
            }
        }
    });

    $.validator.addMethod("alpha", function(value, element){
        return this.optional(element) || value == value.match(/^[a-zA-Z\s]+$/);

    }, "Alphabetic characters only");

});

I also tried it this way, which I found on stackoverflow:

$(document).ready (function (){



    $('#Form').validate({
        rules: {

            firstN:  {
                required: true,
                minlenght: 1,
                field:  {alphaO: "[a-zA-Z]}+"}

            },

            email: {
                required: true,
                email: true
            }
        }
    });

    $.validator.addMethod("alphaO", function(value, element, theRegEx){
        return value.match(new RegExp("^" + theRegEx + "$"));

    }, "Alphabetic characters only");

});
  • Have you looked at [this](http://stackoverflow.com/questions/2476378/jquery-validate-plugin-accept-letters-only)? – dramzy Jul 26 '15 at 16:07
  • yes, updated post to show I've tried that already – Maurice Greenland Jul 26 '15 at 16:20
  • Form validation on `a-z` is only useful if you only ever expect people to have ASCII valid names and contain no spaces etc. Which is not necessarily true at all (José María f.e). Interesting reading: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Jan Jul 26 '15 at 16:31

1 Answers1

0

Ok, think it was a stupid typo that was messing it up. The solution actually works:

$(document).ready (function (){

    $.validator.addMethod("alpha", function(value, element){

        return this.optional(element) || value == value.match(/^[a-zA-Z, '']+$/);

    }, "Alphabetic characters only please");

    $('#Form').validate({
        rules: {
            firstN:  {
                alphaOnly: true,
                required: true,
                minlength: 1
            }
        }
    });


});