Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How can I add phone number validation for specific countries by regex? How can I use if statement for it?

inputRef is the number that I am entering into the input box. it only consists of numbers without country code. first of all I have to choose or type country code and it is gonna be done by . What I am trying to do is that by using regular expressions how I can check if the number is correct for each country individually. I do not know how I have to merge countrycode and phone number from entered value by user and then check it all together. Lets say if it is for United States it has to validate number by US number, if it is for Switzerland it has to check it for Switzerland. I am able to add general regular expression by using pattern:// but I do not know how I can do it for each country separately. in the given code it only tests input with country code but it should not do it because country code has to be taken into consideration from CountryCodeSelector and then regex has to check it. I know this problem is not that much easy but can someone help me to solve it ? Thanks in advance

<div className={classes.phoneInputs} id="phone">
        <FormControl className={classes.phoneSuffix}>
          <Controller
            name="phoneSuffix"
            control={control}
            render={({ onChange, value }) => (
              <CountryCodeSelector value={value} onChange={onChange} />
            )}
          />
         
        </FormControl>
        <Input
          inputRef={register({
            required:true,
             pattern:/^\+(?:[0-9] ?){6,12}[0-9]$/im

           })}
        
          
          name="phone"
          placeholder="XXX XXX XXX"
          className={classes.phoneInput}
          error={!!errors?.phone}
          onBlur={handleBlurPhone}
          data-test="form-phone"
        />
      </div>

I am adding some photos here to express the issue much more detailed: [without anything][1]

it has to only consist of numbers not any sign because country code has to be checked by regex but I do not know how I can do it for each countries seperately

[regex has to take into consideration that country code is there already and it should not work and has to give error but it is not giving error][3]

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!