-1

In here I want to know how to add these symbols + - ( ) for this validation. Currently this section only accept numbers. I want accept both number and these sysmbols + - ( ) . Here is my code

 if (typeof !fields["enterprisemobile"]) {
            isValid = false;
            var pattern = new RegExp(/^[0-9\b]+$/);
            if (!pattern.test(fields["enterprisemobile"])) {
                isValid = false;
                errors["enterprisemobile"] = "Mobile Number is invalid";
            } 
           
        }
D-Nis
  • 29
  • 1
  • 7
  • Does [this](https://stackoverflow.com/questions/22378736/regex-for-mobile-number-validation/22378975) help you – Harshit Rastogi Sep 08 '21 at 09:49
  • Simply add missing characters into your regexp: `new RegExp(/^[0-9\b\+\-\(\)]+$/)` and that kind of validation would still have efficiency close to 0, since something, like `')-9+` would be considered valid phone number. – Yevhen Horbunkov Sep 08 '21 at 09:49
  • @HarshitRastogi : the post you're referring to is focused on phone numbers specific to India, if OP is seeking to validate phone numbers for some other country, validation criteria could be drastically different. – Yevhen Horbunkov Sep 08 '21 at 09:51
  • @YevgenGorbunkov can you please add those symbols with numbers? – D-Nis Sep 08 '21 at 09:57
  • @YevgenGorbunkov because in here I can not add this ( ) characters – D-Nis Sep 08 '21 at 10:00

1 Answers1

1

Replace your regex to accept new symbols. Something like:

 if (typeof !fields["enterprisemobile"]) {
            isValid = false;
            var pattern = new RegExp(/^[0-9\b\+\-\(\)]+$/);
            if (!pattern.test(fields["enterprisemobile"])) {
                isValid = false;
                errors["enterprisemobile"] = "Mobile Number is invalid";
            } 
           
        }

If you want a more accurated explanation of what this regex means you could go here.

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30