Josh Fradley
Josh Fradley

Reputation: 562

Spaces in Jquery Validation Regex

Basically what i am trying to do is validate a form. In one of the fields i want to allow spaces:

I've been using:

$.validator.addMethod(
    "legalname",
    function(value, element) {
        return this.optional(element) || /^[a-zA-Z0-9()._-\s]+$/.test(value);
    },
    "Illegal character. Only points, spaces, underscores or dashes are allowed."
);

$("#editform").validate({
    rules: {
        name: {
            required: true,
            legalname: true
        },
    });

This works in Safari, but not in Firefox where it gives me a "invalid range in character class". Any ideas how i can get this working?

Upvotes: 1

Views: 6670

Answers (1)

gdoron
gdoron

Reputation: 150313

Try escaping the -: As you can see here: Working demo: http://jsfiddle.net/svp6D/2

/^[a-zA-Z0-9()._\-\s]+$/

Characters that need to be escaped inside characters class ([]) are:

- \ / [] ^

Upvotes: 4

Related Questions