Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
Regex for feet and inches - with decimals and fractions

I'm creating an input mask used to for the length of items. This input will convert to a "proper" format on blur but it should accept numbers, decimals, spaces, single quotes, double quotes and / for fractions (no letters).

I've got an okay start, but I'm no regex master and feel I'm complicating my pattern too much. So the following values are allowed:

5 6 (feet and inches separated by spaces)

5'6" (feet and inches in the correct format)

5.2 6 (decimal feet separated by spaces)

5.2'6" (decimal feet in the correct format)

5 6.1 (decimal inches separated by spaces)

5'6.1" (decimal inches in the correct format)

5.2 6.1 (decimal feet and inches separated by spaces)

5.2'6.1" (decimal feet and inches in the correct format)

5 6 1/2 (any combination above followed by a space and fraction)

5.2'6.1 1/2" (again with decimals)

78" (only inches)

78.4" (only inches with a decimal)

Rather picky, I know. I've got something that's a work in progress and I've broken it down to be more readable (to myself at least). http://jsfiddle.net/t37m0txu/383/

// allow numbers
var p_num = "[0-9]";

// numbers are up to 9 characters (it needs a range, for some reason)
var p_range = "{0,9}";

// allow a single decimal
var p_dec = "([.]{0,1})";

// allow a single space (needs to happen only if not directly followed by a decimal)
var p_space = "([ ]{0,1})";

// numbers, possible single decimal and/or space
var p_base = p_num + p_range + p_dec + p_space;

// only allow a single/double quote after a number
var p_afternum = "?(?=" + p_num + ")";

// allow a single or double quote
var p_quote = "(\'(0?" + p_base + ")?\|\"$)";

// issues: 
// i do not need a range/cap on numbers
// after using decimal or space - only one number is allowed to follow (do not cap the range on numbers, only decimal/space)
// do not allow a space directly following a decimal
// do not allow a decimal directly following a single or double quote

var ex = "(" + p_base + ")" + p_afternum + p_quote + "(0?" + p_base + ")?\""
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

How to scan for only valid feet and inches input

This scan uses REGEX masking combined with input mask to create a powerful, feet and inches only textbox.

ex = "[\\d]+(?:\\.[\\d]+|)(?:\\s\\d+\\/\\d+|)(?:\\s|\\'|\\\"|)[\\d]+(?:\\.[\\d]+|)(?:\\s\\d+\\/\d+|)(?:\\'|\\\"|)";
$('#feet').inputmask('Regex', { regex: ex });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
So the following values are allowed:<br /><br />

5 6 (feet and inches separated by spaces)<br />
5'6" (feet and inches in the correct format)<br />
5.2 6 (decimal feet separated by spaces)<br />
5.2'6" (decimal feet in the correct format)<br />
5 6.1 (decimal inches separated by spaces)<br />
5'6.1" (decimal inches in the correct format)<br />
5.2 6.1 (decimal feet and inches separated by spaces)<br />
5.2'6.1" (decimal feet and inches in the correct format)<br />
5 6 1/2 (any combination above followed by a space and fraction)<br />
5.2'6.1 1/2" (again with decimals)<br />
78" (only inches)<br />
78.4" (only inches with a decimal)<br />
    
<input id="feet" />

<br />

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda