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

209
Views
How to automatically take a '/' (slash) in input field using reactJS

How can I format an input to place a slash before the value that the user enters, so that the user cannot delete it because it is already formatted that way. I'm trying to add functionality to input /characters fields so as when the users enters digits, slashes "/" get automatically added before.

I'm new in React, so I don't any idea of how I can resolve that, please helpe me

An Example of the result I want to resolve

enter image description here

 mascaraUserName = (text) => {
        const selectedData_ = this.state.form;
        formated = text;
          if(formated.length==1){
            //formated = text +'/';
            if(selectedData_.userName.indexOf('/') == -1){
              formated = text +'/';
            }
    
          }
    
          console.log(formated);
        // this.setState({ form: myRef }); 
      }

<Col xs={24} sm={24} md={24} lg={24}>
            <FormItem {...formItemLayout}
              label={intl.formatMessage({ id: `maintenace.link.userName` })}>
              {getFieldDecorator(`disable`, {
                initialValue: "https://"
                  ? "https://"
                  : ""
              })(<Input style={{ color: "#3e3e3ed9"}} disabled="disabled"/>)}
              {getFieldDecorator(`userName`, {
                initialValue: selectedData_.userName
                  ? selectedData_.userName
                  : ""
              })(<Input className="inputLink" name="userName" ref={this.myRef} onChange={this.mascaraUserName} placeholder={intl.formatMessage({
                id: `maintenace.link.nicknameIntructions`
              })} />)}
            </FormItem>
          </Col>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can make the input controlled and then intercept any change that tries to remove the initial "/":

Code sandbox

import React, { useState } from "react";

export default function App() {
  const [link, setLink] = useState("/");

  return (
    <input
      value={link}
      onChange={(e) => {
        const value = e.target.value;
        if (!value.startsWith("/")) e.preventDefault();
        else setLink(value);
      }}
    />
  );
}

Edit: Didn't notice this was react native (it's not tagged as it) but I'll leave my answer here since you can probably still use the logic to achieve what you want.

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!