Block sequences of input numbers

Asked

Viewed 400 times

1

Do not allow the filling of numbers in sequence with spaces or without spaces, to avoid adding phone numbers.

Avoid sequences such as:

99999999999 99 9 9999 9999

and any other number having more than 7 numbers sequentially.

The code makes a split and checks each word, if it has more than 7 characters, but, logically, it is functional only with numbers in sequence without space.

        $('input:text, textarea').keyup(function(){
            var num = $(this).val();
            var texto = num.split(" ");
            var a1 = num.split(" ");
            for (i=0;i<a1.length;i++)
            {

                var total_letras = a1[i].length;
                if($.isNumeric(a1[i])){
                    if( $(this).attr("id") !== 'input_cep' )//se id for cep, permitirá a digitação
                    {
                        if(total_letras > 7)
                        {           
                            $(this).val( num.substring(0, num.length - 2) );                        
                            $(this).addClass("input_bloqueado");
                        }
                        else
                        {
                            $(this).removeClass("input_bloqueado");
                        }
                    }
                }
            }
        });
  • What value should return ? Example: 11999933333333 2 3333 1 77777 777 1

  • @Maurydeveloper the idea is to block the typing of any numerical sequence larger than 7 digits, with or without spaces

  • Do you want the answer with Jquery or pure Javascript? Note: I hate jQuery,.

  • without preference, can be javascript

  • textarea takes more than one 'field'? attribute maxlength doesn’t help him?

  • 1

    var reg = /([0-9]{7})/g; str.replace(" ","").replace(reg,""); Removes sequence of 7 in a row

  • https://answall.com/questions/295543/howto Detect One Determinatedsequence OneJavascript Also similar

  • @Maurydeveloper saved in jsfiddle or as a response

  • Worked that code?

  • https://jsfiddle.net/sNniffer/3ksdguw7/1/ - Text interference appears to be present or is not correctly implemented

  • True. I’ll try another way.

Show 6 more comments

3 answers

0

var inputText = window.textA || undefined

if( !inputText ){
input = document.getElementsByTagName('input')
for( i=0; i < input.length; ++i ) if( input[i].type.match(/text/) ) var inputText = input[i]
}

var textarea = window.textB || document.getElementsByTagName('textarea')[0]

inputText.onkeyup = function(){ checkValueA(this) }
textarea.onkeyup  = function(){ checkValueB(this) }

var lastInputText, lastAreaText

function checkValueA(){
/* arguments[0].maxLength = 7 */
arguments[0].value = arguments[0].value.match(/[0-9]{1}$/)? arguments[0].value : (lastInputText||'')
lastInputText = arguments[0].value
if( arguments[0].value.match(/^[0-9]{7}$/) ) getNumber(arguments[0])
}

function checkValueB(){
/* arguments[0].maxLength = 7 */
arguments[0].value = arguments[0].value.match(/[0-9]{1}$/)? arguments[0].value : (lastAreaText ||'')
lastAreaText = arguments[0].value
if( arguments[0].value.match(/^[0-9]{7}$/) ) getNumber(arguments[0])
}

function getNumber(){
arguments[0].disabled = true
/* arguments[0].class = 'input_bloqueado' */
numero = +arguments[0].value

if( arguments[0].type == 'text'     ) labelA.innerHTML = '7 digitos sem espaços ou letras ' + labelA.innerText
if( arguments[0].type == 'textarea' ) labelB.innerHTML = '7 digitos sem espaços ou letras ' + labelB.innerText
if( arguments[0].type == 'text'     ) { labelA.style.color = "red"; labelA.appendChild( arguments[0] ) }
if( arguments[0].type == 'textarea' ) { labelB.style.color = "red"; labelB.appendChild( arguments[0] ) }
}
<style>
input[type=text], textarea {
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
font-size: 11pt;
width: 300px;
height: 46px;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
resize: none;
display: block;
}
</style>

<label id="labelA">( input text ):
<input type="text" placeholder="Telefone#" />
</label>
<label id="labelB">( textarea ):
<textarea placeholder="Telefone#"></textarea>
</label>

0


Solulção:

$('#texto').keyup(function(){
    var reg = /((?:[1-9]\s*){7})/g;
    var texto = $(this).val();
    var result = texto.replace(reg,"********") ;

    $("#texto").val( result );

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="texto" id="texto"/>

-2

form, input[type=number], input[type=submit] {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 11pt;
border-radius: 4px;
display: block;
}

input[type=number] {
width: 300px;
height: 47px;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid #ccc;
-moz-appearance: textfield;
-webkit-appearance: none;
}

input[type=submit] {
background-color: red;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
}
<form>
Número de telefone: <input type="number" name="quantity" min="9000000" max="9999999" title="7 digitos">
<input type="submit">
</form>

HTML <input type="number"> min max Attribute https://www.w3schools.com/tags/att_input_max.asp
HTML <input type="text"> Pattern Attribute https://www.w3schools.com/tags/att_input_pattern.asp

  • Have you forgotten phone number has 9 digits? Example: 988887777 min: 900000000 max: 999999

  • "Avoid sequences such as: (...)(..) and any others that have more than 7 numbers in sequence."... in the question.

  • But it is wrong. Sorry for the vote against. Number is 8 or at least type

  • There are 14 in Angola: https://www.tellows.pt/num/00244922781982

  • Yes, but I mentioned Brazil. Angola I love this country .

  • There are 7 in Cape Verde: https://www.paginasamarelas.cv/en/business-details/restaurants/2176518-restaurante-grill-luar

  • Let’s chat? This conversation comes out of the question.

  • See the solution of the questioner... /(?: [1-9] s*){7})/g; ... 7(seven).

  • checks each word if it has more than 7 characters. Have you not seen the text of the question? Something else she did not ask for phone and yes zip code. if( $(this).attr("id") !== 'input_cep' )//se id for cep, permitirá a digitação

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.