-2

I want to restrict a phone number field to only 'numbers', '+' and 'spaces', or restrict 'A-Z'. I am using jQuery Validate and I have tried:

jQuery.validator.addMethod("numero", function(value, element) {
    return this.optional(element) || value == value.match(/^[+1-9 ]*$/);
});

However the regex for my numero rule doesn't seem to be working correctly.

My code:

jQuery(document).ready(function(){

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


    jQuery.validator.addMethod("numero", function(value, element) {
        return this.optional(element) || value == value.match(/^[+1-9 ]*$/);
    });

    jQuery('.upsale-form').each(function() {
        jQuery(this).validate({
            rules: {
                'Contact.name': {
                    minlength: 3,
                    required: true,
                    alpha: true
                },
                'Contact.email': {
                    required: true,
                    email: true
                },
                'Contact.phone': {
                    minlength: 9,
                    required: true,
                    numero: true
                }
            }
        });   

    });
});
Sparky
  • 98,165
  • 25
  • 199
  • 285
Graham Barnes
  • 235
  • 2
  • 6
  • 18
  • What does not seem to be working? The validation is completely broken? Your regex is not working? Otherwise, your code is working for me: http://jsfiddle.net/2dx9jkn2/ – Sparky Mar 01 '16 at 23:29
  • **NOTES**: 1. You would not put the `.addMethod()` methods inside of the `.each()`. These are simply creating new rules that can be used anyplace and do not need to be called more than once on the page. 2. You forgot to define the error messages for your custom rules. See: http://jsfiddle.net/pqjfxbyt/ – Sparky Mar 01 '16 at 23:32
  • Thanks, I tried to put '+4400000000' in the phone number field but its not working and if I put '44 0000 0000' it still displays the error 'numbers only' in the 2nd form on your 2nd fiddle. I am quite new to this level of validation so I was guessing a lot of this, in regards to the messages are those needed as I don't want them to display on the page, I am using css to highlight the invalid fields. – Graham Barnes Mar 02 '16 at 12:41
  • Use the "edit" button to clarify this information in your OP as per my previous comments. As you wrote it, the OP did not explain "not working". Since you apparently only need help with writing the `regex` portion, I've updated your tags. If you don't want the error messages to display, then you'd put a `return false` inside of the `errorPlacement` callback function and all messages will be suppressed. – Sparky Mar 02 '16 at 15:44
  • 1
    Possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) –  Mar 02 '16 at 15:48

3 Answers3

1

I want to restrict a phone number field to only 'numbers', '+' and 'spaces'

Your regex, /^[+1-9 ]*$/, was only matching 1 through 9 and leaving out any zeros. You could use \d to represent any digit instead.

So /^[\d+ ]*$/ will work.

See: http://jsfiddle.net/pqjfxbyt/1/

Reference: regexr.com

Sparky
  • 98,165
  • 25
  • 199
  • 285
1

Maybe you want "0-9" and not "1-9"?

return this.optional(element) || value == value.match(/^[+0-9 ]*$/);
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
0

Try this Regx this will help you out ([0-9+-])\w+