Git Product home page Git Product logo

mask-for-html's Introduction

- Mask for HTML -

A simple function for you to use in your inputs and be able to edit them according to your preference while the user is typing!

Language

function mask(selector, mask, regex) {
    if (regex) {
        document.querySelector(selector).setAttribute('pattern', regex);
    }

    document.querySelector(selector)
    .addEventListener('keypress', () => {
        const text = document.querySelector(selector);
        addValue(mask, text.value.length, text);

    });

    function addValue(origin, now, input) {
        if (origin[now] && origin[now] !== '0') {
            input.value += origin[now];

            if (origin[now +1] && origin[now +1] !== '0') {
                addValue(origin, now +1, input);
            }
        }
    }
}

PORTUGUÊS

Como usar

A função aceita 3 parametros

  • selector (obrigatório): ID ou classe do elemento a ser editado;
  • mask (obrigatório): Padrão desejado para a edição, usando como base números zeros (0);
  • regex (opcional): Expressão Regular para ser adicionada ao pattern
    Observação: Se você for passar a regex como parâmetro, lembra passa-lá como string e de duplicar as / para que a sua string não fique "deformada".

Exemplos:


Código:

<body>
  <label>
    <p>CPF:</p>
    <input type="text" name="cpf" id="cpf" placeholder="Digite seu telefone (00) 00000-0000" required>
  </label>
</body>
<script>
  // cole a função aqui
  
  // //  selector       mask     regex (com as `/` duplicadas e em formato string)
  mask('#cpf', '000.000.000-00', '[0-9]{3}\\.[0-9]{3}\\.[0-9]{3}-[0- 9]{2}');
</script>

Resultado:

mask_input

Código:

<body>
  <label>
    <p>Telefone:</p>
    <input type="text" name="telefone" class="telefone" id="telefone" placeholder="Digite seu telefone (00) 00000-0000" required>
  </label>
</body>
<script>
  // cole a função aqui
  
  //       selector        mask
  mask('.telefone', '(00)00000-0000');
</script>

Resultado:

nask_input

Nos dois casos eu digitei apenas os números

ENGLISH

How to use

The function accepts 3 parameters

  • selector (mandatory): ID or class of the element to be edited;
  • mask (mandatory): Desired pattern for editing, using zeros (0) as a base;
  • regex (optional): Regular expression to be added to the pattern
    Note: If you are going to pass the regex as a parameter, remember to pass it as a string and to double the / so that your string is not "deformed".

Examples:


Code:

<body>
  <label>
    <p>CPF:</p>
    <input type="text" name="cpf" id="cpf" placeholder="Digite seu telefone (00) 00000-0000" required>
  </label>
</body>
<script>
  // pastle the function here
  
  // mask regex selector (with duplicate `/` and in string format)
  mask('#cpf', '000.000.000-00', '[0-9]{3}\\.[0-9]{3}\\.[0-9]{3}-[0- 9]{2}');
</script>

Result:

mask_input

Code:

<body>
  <label>
    <p>Telefone:</p>
    <input type="text" name="phone" class="phone" id="phone" placeholder="Digite seu telefone (00) 00000-0000" required>
  </label>
</body>
<script>
  // pastle the function here

  // mask selector
  mask('.phone', '(00)00000-0000');
</script>

Result:

nask_input

In both cases I just typed the numbers

Autor


Felipe Fuckner Clariano

Entre em contato! 💌

Linkedin Badge Gmail Badge

mask-for-html's People

Contributors

fuckners avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.