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

241
Views
Do not repeat tags

On my site, I'm using TagsInput, which allows the user to enter data into an input field, hit the enter button, and see it displayed as tags.

But I have one problem: the user can enter data with the same value as many times as he wants. I would like to restrict this ability and not allow the same data to be entered.

I already have some validation that displays a message if the user has entered an invalid data format.

Thus, I would like to add the ability to not accept data if it is already in the tags and display the corresponding message.

export default function TagsInputRequestURL(props) {
    const {tags, setTags} = props;
    const [input, setInput] = useState("");
    const [isValid, setIsValid] = useState(true);
  
    const onChange = (e) => {
      const { value } = e.target;
      if (e.target.value) {
        setIsValid(() => /^(ftp|https?):\/\/[^ "]+$/.test(e.target.value));
      } else {
        setIsValid(true);
      }
      setInput(value);
    };
  
    const onSubmit = (e) => {
      e.preventDefault();
      if (isValid) {
        setTags((tags) => [...tags, input]);
        setInput("");
      }
    };
  
    const deleteTag = (index) => {
      setTags((prevState) => prevState.filter((tag, i) => i !== index));
    };
  
    return (
      <div className={classes.container}>
        {tags.map((tag, index) => 
          <div className={classes.tag}>
            <ClearIcon
              className={classes.del}
              fontSize="big"
              onClick={() => deleteTag(index)}
            />
            {tag}
          </div>
        )}
        <form   onSubmit={onSubmit}>
          <input
            className={classes.input}
            value={input}
            placeholder={props.inputPlaceholder}
            onChange={onChange}
          />
          {!isValid && <small style={{ color: "red" }}>Invalid URL</small>}
        </form>
      </div>
    );
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

export default function TagsInputRequestURL(props) {
    const {tags, setTags} = props;
    const [input, setInput] = useState("");
    const [isValid, setIsValid] = useState(true);
  
    const onChange = (e) => {
      const { value } = e.target;
      if (e.target.value) {
        setIsValid(() => /^(ftp|https?):\/\/[^ "]+$/.test(e.target.value));
      } else {
        setIsValid(true);
      }
      setInput(value);
    };

    const containsString = (str) => {
        if(!str || str === '') return false
        const strLower = str.toLowerCase();
        let isExist = false
        for(let i=0; i<tags.length; i++){
            let itemLower = tags[i].toLowerCase();
            if(strLower === itemLower){
                isExist = true;
                break;
            }
        }
        return isExist;
    }
  
    const onSubmit = (e) => {
      e.preventDefault();
      if (isValid && !containsString(input)) {
        setTags((tags) => [...tags, input]);
        setInput("");
      }
      else{
        console.log("You already hame same value in the 'tags' array. Try with different string.")
      }
    };
  
    const deleteTag = (index) => {
      setTags((prevState) => prevState.filter((tag, i) => i !== index));
    };
  
    return (
      <div className={classes.container}>
        {tags.map((tag, index) => 
          <div className={classes.tag}>
            <ClearIcon
              className={classes.del}
              fontSize="big"
              onClick={() => deleteTag(index)}
            />
            {tag}
          </div>
        )}
        <form onSubmit={onSubmit}>
          <input
            className={classes.input}
            value={input}
            placeholder={props.inputPlaceholder}
            onChange={onChange}
          />
          {!isValid && <small style={{ color: "red" }}>Invalid URL</small>}
        </form>
      </div>
    );
  }
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!