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

160
Views
How to validate phone number in react that until user give valid phone number button should disabled
import React,{useState} from "react";
export default function Validate() {
  const [value, setValue] = useState("");
  const [disable, setDisable] = useState(false);
  function handleChange(e) {
    setValue(e.target.value);
    if (e.target.value.length <10)
     {
      setDisable(true);
    }
  }
  return (
    <>
      Phn:<input error={disable} type="number" value={value} onChange={handleChange}/>
      <button disabled={!disable}>click</button> 
    </>
  );
}

in this case initially button is disabled but when i type one number button is enabled but i need untill user give 10 digit phone number button is disabled

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

import React,{useEffect, useState} from "react";
export default function Validate() {
  const [value, setValue] = useState("");
  const [disable, setDisable] = useState(true);
  function handleChange(e) {
    setValue(e.target.value);
  }
  useEffect(() => {
    setDisable(value.length !== 10);
  }, [value])
  return (
    <>
    Phn:<input error={disable} type="number" value={value} onChange={handleChange}/>
    <button disabled={disable}>click</button> 
    </>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

You can try this:

I have added the link to the code https://codesandbox.io/s/happy-swartz-ikqdn?file=/src/input.js

Go to https://ikqdn.csb.app/input in codesandbox browser


What you are missing in the code is

function handleChange(e) {
    setValue(e.target.value);
    if (e.target.value.length <10) <-- not updating the disable if length = 10
     {
      setDisable(true); 
    }
  }

and in the input and button error and disable

Phn:<input error={disable} type="number" value={value} onChange={handleChange}/>
<button disabled={!disable}>click</button> 

Both are in alternative state, i.e. if one becomes valid, then other becomes invalid.


Initially disabled must be true because input is empty and

I have taken initial value of input as null as an indicator of not clicked.

This is the condition of input error Input is clicked and the length != 10

value !== null && disable
about 4 years ago · Juan Pablo Isaza Report
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!