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";
            } 
           
        }
5
  • Does this help you Commented Sep 8, 2021 at 9: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. Commented Sep 8, 2021 at 9: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. Commented Sep 8, 2021 at 9:51
  • @YevgenGorbunkov can you please add those symbols with numbers? Commented Sep 8, 2021 at 9:57
  • @YevgenGorbunkov because in here I can not add this ( ) characters Commented Sep 8, 2021 at 10:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.